Solved

php insert from form

Posted on 2014-04-22
5
421 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 110

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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.

762 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