Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

If Else Statement / PHP Form

Codes here !

Moderators: macek, egami, gesf

If Else Statement / PHP Form

Postby michelle2175 » Wed Jan 23, 2013 8:43 am

Hello,

I have a form where the user can add inventory into a table within mysql db. I am trying to setup it up so that the user can submit two items at a time.

I have the form working when adding just one item and actually it is also working for adding two items.

The issue I am having is around it adding an empty row is the user does not complete the section for the second item.

Here is my code:

Code: Select all
... connect to db code

$category = check_input($_POST['category']);
$title = check_input($_POST['title']);
$issue = check_input($_POST['issue']);
$date = check_input($_POST['date']);
$price = check_input($_POST['price']);
$book = check_input($_POST['book']);

$category2 = check_input($_POST['category2']);
$title2 = check_input($_POST['title2']);
$issue2 = check_input($_POST['issue2']);
$date2 = check_input($_POST['date2']);
$price2 = check_input($_POST['price2']);
$book2 = check_input($_POST['book2']);

//Create INSERT query
$qry = "INSERT INTO inventory(category, title, issue, date, price, book, image)
VALUES('$category', '$title', '$issue', '$date', '$price', '$book')";

$qry2 = "INSERT INTO inventory(category, title, issue, date, price, book, image)
VALUES('$category2', '$title2', '$issue2', '$date2', '$price2', '$book2'";

$result = @mysql_query($qry);
$result2 = @mysql_query($qry2);

//Check whether the query was successful or not
if($result && $result2) {
header("location: survey-success1.php");
exit();
}else {
die("Query failed");
   }
?>


I tried the if statement below but it throws me a "Query failed"
Code: Select all
if($name2 == !null){
$qry2 = "INSERT INTO inventory(category, title, issue, date, price, book, image)
VALUES('$category2', '$title2', '$issue2', '$date2', '$price2', '$book2', '$file_name2')";


If anyone has any suggestions, I would greatly appreciate them! :)
michelle2175
New php-forum User
New php-forum User
 
Posts: 2
Joined: Mon Sep 12, 2011 3:57 pm

Re: If Else Statement / PHP Form

Postby php9 » Fri Feb 22, 2013 3:13 am

Well you can check if the user entered some content into a field using:

Code: Select all
if(!empty($_POST['something']))
{
    // user entered some content
}


Using that you can have your script only do the insert when there is some content.
php9
New php-forum User
New php-forum User
 
Posts: 13
Joined: Fri Feb 22, 2013 2:20 am


Return to mySQL & php coding

Who is online

Users browsing this forum: Google [Bot] and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron