Solved

php insert from form

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

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
That doesn't answer my question.
0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 105 total points
Comment Utility
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
Comment Utility
if (empty($rent)) {$rent = '0';}

That will work.
0
 

Author Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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 …
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now