Solved

php insert from form

Posted on 2014-04-22
5
438 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

636 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