Solved

php insert from form

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to set the Tinymce Editor image path 4 22
PHP breaks when used in Wordpress template file 3 30
Where on a calculated field 1 20
ajax to record click 3 16
As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
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.

809 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