Solved

php insert from form

Posted on 2014-04-22
5
409 Views
Last Modified: 2014-04-22
If I'm inserting data into a database through php. Is there any way the users doesn't have to full out all the form fields?

Why can't I just assign to 0 so if it's left empty it will insert a 0. However when I do that I get no results when it tries to update data.

      
$rent=0;
$food=0;
$gas=0;
$clothes=0;
$supplies=0;

      if (isset($_POST['submit'])){
      $rent = ($_POST["rent"]);
      $food = $_POST["food"];
      $gas = ($_POST["gas"]);
      $clothes = $_POST["clothes"];
      $supplies = $_POST["supplies"];      
}

$queryUpdate = "update january set rent=$rent, food=$food, gas=$gas, clothes=$clothes, supplies=$supplies where customerid='$id'";                              
                                                      $result = mysqli_query($connection, $queryUpdate);
                                                      if (!$result){
                                                            die("doesn't update " .
                                                            mysqli_connect_error() .
                                                            "(" . mysqli_connect_errno() . ")"
                                                            );            
                                                                        }
0
Comment
Question by:designaire
  • 3
5 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40016504
If you're new to PHP and SQL this article will help you get a foundation in how it all works.
http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
0
 

Author Comment

by:designaire
ID: 40016530
That doesn't answer my question.
0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 105 total points
ID: 40016565
Currently, the sql is failing because syntax like this (with missing values) is invalid:
update my_table set value1 =, value2= where id=1

Open in new window


You can easily add a line to check for an empty value, and set it to 0 eg:
if (empty($rent)) {$rent = '0';}

Open in new window

If this code is exposed to the internet, or to untrusted users, then you'll also want to escape your values to prevent SQL injection attacks. In that case you should also be logging or reporting SQL errors.
0
 

Author Comment

by:designaire
ID: 40016601
if (empty($rent)) {$rent = '0';}

That will work.
0
 

Author Comment

by:designaire
ID: 40016603
The code was working, it just didn't work one of the fields weren't filled in.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question