PHP/SQL Inserting data from form into two tables

Hi all
I wonder if someone can help me as this is driving me mad.

I need to transfer data from one html form into two sql databases using php. I have written the below PHP code to do this:

[php]

<?php $con = mysql_connect("localhost","Username","Password"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("m09ewt2", $con); $sql= "INSERT INTO Return_Reason (Serial_Number, Date_Issued ,Date_Recieved, Reason, Date_Returned, Outcome) VALUES ('$_POST[Serial_Number]','$_POST[Date_Issuued]','$_POST[Date_Recieved]','$_POST[Reason]','$_POST[Date_Returned]','$_POST[Outcome]')"; $sql1= "INSERT INTO Customer_Product (Serial_Number,Product_ID,User_ID, Customer_ID) VALUES ('$_POST[Serial_Number]','$_POST[Product_ID]','$_POST[User_ID]','$_POST[Customer_ID]' )"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added"; mysql_close($con) ?>

[/php]

This code works fine when I submit data to one table. However when I use this code to submit data it just puts null in every record in the database.

Can anyone tall me why. If you require more info please say.

Thanks in advance

Edd

you forgot your squirrelly braces, when inserting post data you have to add curly braces around the post part:
[php]

<?php $con = mysql_connect("localhost","Username","Password"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("m09ewt2", $con); $sql= "INSERT INTO Return_Reason (Serial_Number, Date_Issued ,Date_Recieved, Reason, Date_Returned, Outcome) VALUES ('{$_POST['Serial_Number']}','{$_POST['Date_Issuued']}','{$_POST['Date_Recieved']}','{$_POST['Reason']}','{$_POST['Date_Returned']}','{$_POST['Outcome']}')"; $sql1= "INSERT INTO Customer_Product (Serial_Number,Product_ID,User_ID, Customer_ID) VALUES ('{$_POST['Serial_Number']}','{$_POST['Product_ID']}','{$_POST['User_ID']}','{$_POST['Customer_ID']}' )"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added"; mysql_close($con) ?>

[/php]
oh also it look like you never actually do the query to insert $sql1 so you might want to add that part in! 8)

Hi

Many thanks for your reply.

However even when I use this it still inserts null values. I cant understand why it is doing this.

Any other Ideas?

Kind Regards

Ed

can you post the code for the form that you are using to post data I want to see if the everything is correct there as well

Hi

Thanks for not giving up.

My code from the form is below:


<table style="color: #FFFFFF;" border = "1" width = "30%">

 <tr> 
      <td>Issued By:</td>
  <td>

<?php 
$server="localhost";
$username="Username";
$password="Password";
$database="DB";
 
$connection = mysql_connect($server, $username, $password) or die('Could not connect'.mysql_error());
mysql_select_db($database, $connection) or die("Cannot select db.");
      

$sql="SELECT User_ID, UserName FROM Users";
$result=mysql_query($sql);

$options="";

while ($row=mysql_fetch_array($result)) {

    $User_ID=$row["User_ID"];
    $UserName=$row["UserName"];
    $options.="<OPTION VALUE=\"$User_ID\">".$UserName;
}
?>

<SELECT NAME=UserName>
<OPTION VALUE=0>Choose
<?=$options?>
</SELECT> 
</td>
<td>

     <form action="AddUser">
<button type="submit" name="AddUser" value="clicked">
+
</button>
</form> 
 </td>

<tr>
<td>
Date Issued:
</td>

<td>
<input type="date" name="Date_Issued" size="18" maxlength="100">
</td>

</tr>



<tr>
<td> 
Customer Name: 
</td>
<td>

<?php 
$server="localhost";
$username="Username";
$password="Password";
$database="DB";
 
$connection = mysql_connect($server, $username, $password) or die('Could not connect'.mysql_error());
mysql_select_db($database, $connection) or die("Cannot select db.");
      

$sql="SELECT Customer_ID, CustomerName FROM Customer";
$result=mysql_query($sql);

$options="";

while ($row=mysql_fetch_array($result)) {

    $Customer_ID=$row["Customer_ID"];
    $CustomerName=$row["CustomerName"];
    $options.="<OPTION VALUE=\"$Customer_ID\">".$CustomerName;
}
?>

<SELECT NAME=CustomerName>
<OPTION VALUE=0>Choose
<?=$options?>
</SELECT> 
</td>

</form>





<td> 
<form action="AddCustomer">
<button type="submit" name="AddCustomer" value="clicked">
+
</button>
</form> 
<td> 
<form action="CustomerTable.php">
<button type="submit" name="CustomerTable" value="clicked">
<img src="Table.gif" width="20" height="20" alt="submit" border="0" />
</button>
</form> 
</td> 

</td> 
    </tr>
    <tr> 
   
       
      </td>
    </tr>
<tr> 
    <td>
Product: 
</td>
<td>


<?php 
$server="localhost";
$username="username";
$password="password";
$database="db";
 
$connection = mysql_connect($server, $username, $password) or die('Could not connect'.mysql_error());
mysql_select_db($database, $connection) or die("Cannot select db.");
      

$sql="SELECT Product_ID, Product_Name FROM RMAProduct";
$result=mysql_query($sql);

$options="";

while ($row=mysql_fetch_array($result)) {

    $Product_ID=$row["Product_ID"];
    $Product_Name=$row["Product_Name"];
    $options.="<OPTION VALUE=\"$Product_ID\">".$Product_Name;
}
?>

<SELECT NAME=ProductName>
<OPTION VALUE=0>Choose
<?=$options?>
</SELECT> 

</td>
<td>
     <form action="AddProduct.php">
<button type="submit" name="AddProduct" value="clicked">
+
</button>
</form> 
 </td>
<td> 
<form action="ProductTable.php">
<button type="submit" name="ProductTable" value="clicked">
<img src="Table.gif" width="20" height="20" alt="submit" border="0" />
</button>
</form> 
</td> 

</tr>

<tr> 
      <td>Serial No.</td>
      <td> 
        <input type="text" name=" Serial No" size="18" maxlength="100">
      </td>
    </tr>

<tr>
<td>
Date Recieved:
</td>

<td>
<input type="date" name="Date_Returned" size="18" maxlength="100">
</td>

</tr>
<tr>
<td>
Reason:
</td>

<td>
<input type="text" name="Reason" size="18" maxlength="100">
</td>

</tr>
<tr>
<td>
Outcome:
</td>

<td>
<input type="text" name="Outcome" size="18" maxlength="100">
</td>

</tr>
<tr>
<td>
Date Returned:
</td>


<td>
<input type="date" name="Date_Returned" size="18" maxlength="100">
</td>
</tr>


    <tr>
    <td>
</td>
      <td>
<form name="AddRecord" method="post" action="InsertRecord.php">
        <input type="submit" name="Submit" value="Submit">
      </td>

      <td>&nbsp;</td>
    </tr>
  </table>
</form>

Thanks again.

Edd

ok first thing if you are not connecting to different databases then you only need to connect to the database once, so you put that connection at the top of the page, then select from the different tables as needed, your connection to the database will stay open the whole time you are on the page! give me sec to review this and I will clean up the code and give you a revised edition

ok so here is your problem you added all these different actions to different forms so you are trying to send one piece of information to one page another to another page and so on, there is no way the database can collect it all, next you have to input fields both named Date_Returned when one should have been Date_Recieved.
This form really is a mess.
From what I gather by reading this you are making something that will record when a product is returned you need to save the following info:
issued by: User_Name
Date Issued: Date_Issued
Customer Name: Customer_Name
Product: Product_Name
Serial No: Serial_No
Date Recieved: Date_Recieved
Reason: Reason
Outcome: Outcome
Date Returned: Date_Returned
an easy way to do this is write everythign you need down on a piece of paper that you need as I did above on the left, then choose what you are going to name each field in the database as I did above on the right, now we need to create the correct database table because I do not think you did ;D
so lets create a new datebase table open up your database and run this sql:
[php]
CREATE TABLE productreturn (
id INT( 50 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
User_Name VARCHAR( 50 ) NOT NULL ,
Date_Issued VARCHAR( 25 ) NOT NULL ,
Customer_Name VARCHAR( 50 ) NOT NULL ,
Product_Name VARCHAR( 50 ) NOT NULL ,
Serial_No VARCHAR( 50 ) NOT NULL ,
Date_Recieved VARCHAR( 50 ) NOT NULL ,
Reason TEXT NOT NULL ,
Outcome TEXT NOT NULL ,
Date_Returned VARCHAR( 50 ) NOT NULL ,
INDEX ( User_Name , Customer_Name )
) ENGINE = MYISAM ;
[/php]
now we are going to put EVERYTHING on the form page, there is no need for addition pages so create your productreturn.php I did not test this I just wrote it up really quick there is a better way to write out the striptags and escape string part but for now I think you should just see the basic way. so in product return add the following and let me know if everything works correctly!
[php]

<?php $server="localhost"; $username="Username"; $password="Password"; $database="DB"; $connection = mysql_connect($server, $username, $password) or die('Could not connect'.mysql_error()); mysql_select_db($database, $connection) or die("Cannot select db."); if($_POST['productreturn']=="welostmoney"){ $username=mysql_strip_tags($_POST['User_Name']); $username=mysql_escape_string($username); $dateissued=mysql_strip_tags($_POST['Date_Issued']); $dateissued=mysql_escape_string($dateissued); $customername=mysql_strip_tags($_POST['Customer_Name']); $customername=mysql_escape_string($customername); $productname=mysql_strip_tags($_POST['Product_Name']); mysql_escape_string($productname); $serialno=mysql_strip_tags($_POST['Serial_No']); $serialno=mysql_escape_string($serialno); $daterecieved=mysql_strip_tags($_POST['Date_Recieved']); $daterecieved=mysql_escape_string($daterecieved); $reason=mysql_strip_tags($_POST['Reason']); $reason=mysql_escape_string($reason); $outcome=mysql_strip_tags($_POST['Outcome']); $outcome=mysql_escape_string($outcome); $datereturned=mysql_strip_tags($_POST['Date_Returned']); $datereturned=mysql_escape_string($datereturned); mysql_query("INSERT INTO `productreturn` (`id`, `User_Name`, `Date_Issued`, `Customer_Name`, `Product_Name`, `Serial_No`, `Date_Recieved`, `Reason`, `Outcome`, `Date_Returned`) VALUES (NULL, '$username', '$dateissued', '$customername', '$productname', '$serialno', '$daterecieved', '$reason', '$outcome', '$datereturned')"); $msg="Inserted Successfully!!! "; } ?> <? echo $msg; ?>
Issued By: <?php $sql="SELECT User_ID, UserName FROM Users"; $result=mysql_query($sql); $options=""; while ($row=mysql_fetch_array($result)) { $User_ID=$row["User_ID"]; $UserName=$row["UserName"]; $options.="".$UserName; } ?> Choose <?=$options?>
Date Issued:
Customer Name: <?php $sql="SELECT Customer_ID, CustomerName FROM Customer"; $result=mysql_query($sql); $options=""; while ($row=mysql_fetch_array($result)) { $Customer_ID=$row["Customer_ID"]; $CustomerName=$row["CustomerName"]; $options.="".$CustomerName; } ?> Choose <?=$options?>
Product: <?php

$sql=“SELECT Product_ID, Product_Name FROM RMAProduct”;
$result=mysql_query($sql);
$options="";
while ($row=mysql_fetch_array($result)) {
$Product_ID=$row[“Product_ID”];
$Product_Name=$row[“Product_Name”];
$options.="<OPTION VALUE="$Product_ID">".$Product_Name;
}
?>

Choose <?=$options?>
Serial No.
Date Recieved:
Reason:
Outcome:
Date Returned:
    <input type="submit" name="Submit" value="Submit">
  </td>
  
</tr>
[/php]

whoops scrap that if you need it to go into two database tables LOL I just reread your posting! I will write you a new one give me a sec :stuck_out_tongue:

ok sorry about that since you already have the two database tables, I am hoping to god that you have the correct fields in them, you only needed on table though :smiley: but since you have two here is how to update them both again call this script productreturn.php:
[php]

<?php $server="localhost"; $username="Username"; $password="Password"; $database="DB"; $connection = mysql_connect($server, $username, $password) or die('Could not connect'.mysql_error()); mysql_select_db($database, $connection) or die("Cannot select db."); if($_POST['productreturn']=="welostmoney"){ $username=mysql_strip_tags($_POST['User_Name']); $username=mysql_escape_string($username); $dateissued=mysql_strip_tags($_POST['Date_Issued']); $dateissued=mysql_escape_string($dateissued); $customername=mysql_strip_tags($_POST['Customer_Name']); $customername=mysql_escape_string($customername); $productname=mysql_strip_tags($_POST['Product_Name']); mysql_escape_string($productname); $serialno=mysql_strip_tags($_POST['Serial_No']); $serialno=mysql_escape_string($serialno); $daterecieved=mysql_strip_tags($_POST['Date_Recieved']); $daterecieved=mysql_escape_string($daterecieved); $reason=mysql_strip_tags($_POST['Reason']); $reason=mysql_escape_string($reason); $outcome=mysql_strip_tags($_POST['Outcome']); $outcome=mysql_escape_string($outcome); $datereturned=mysql_strip_tags($_POST['Date_Returned']); $datereturned=mysql_escape_string($datereturned); mysql_query("INSERT INTO Return_Reason (Serial_Number, Date_Issued ,Date_Recieved, Reason, Date_Returned, Outcome) VALUES ('$serialno','$dateissued','$daterecieved','$reason','$datereturned','$outcome')"); mysql_query("INSERT INTO Customer_Product (Serial_Number,Product_ID,User_ID, Customer_ID) VALUES ('$serialno','$productid','$username','$customername' )"); $msg="Inserted Successfully!!!"; } ?> <? echo $msg; ?>
Issued By: <?php $sql="SELECT User_ID, UserName FROM Users"; $result=mysql_query($sql); $options=""; while ($row=mysql_fetch_array($result)) { $User_ID=$row["User_ID"]; $UserName=$row["UserName"]; $options.="".$UserName; } ?> Choose <?=$options?>
Date Issued:
Customer Name: <?php $sql="SELECT Customer_ID, CustomerName FROM Customer"; $result=mysql_query($sql); $options=""; while ($row=mysql_fetch_array($result)) { $Customer_ID=$row["Customer_ID"]; $CustomerName=$row["CustomerName"]; $options.="".$CustomerName; } ?> Choose <?=$options?>
Product: <?php

$sql=“SELECT Product_ID, Product_Name FROM RMAProduct”;
$result=mysql_query($sql);
$options="";
while ($row=mysql_fetch_array($result)) {
$Product_ID=$row[“Product_ID”];
$Product_Name=$row[“Product_Name”];
$options.="<OPTION VALUE="$Product_ID">".$Product_Name;
}
?>

Choose <?=$options?>
Serial No.
Date Recieved:
Reason:
Outcome:
Date Returned:
    <input type="submit" name="Submit" value="Submit">
  </td>
  
</tr>
[/php] hope this works for ya bud!

Hi Plinto

Many thanks for replying twice lol,

Sorry for the delay in my reply I have been away this weekend.

I have decided not to overcomplicate things and have all of my data in the one table.

I have used both your SQL script to create the table which is fine. However when I complete the form and click submit all that seems to happen is the form refreshes. I do not get the “inserted successfully message” and none of the data appears in the SQL table.

Any Ideas.

Thanks again

Ed

did you copy and paste exactly or did you change ANYTHING in the script because I ran it and tested it before I posted it here. if you changed any of this :
[php]

[/php] it will fail, because of this part of the code: [php] if($_POST['productreturn']=="welostmoney"){ [/php] try it out with out changing any of the values of what I put there and see how it runs

Hi,

The only details I have changed are the connection details to connect to the DB.

Other than that I have copied and pasted the code exactly. All that seems to happen is that the form refreshes when I click the submit button.

I am baffled!

Kind Regards

Ed

sorry I said I tested this one but this is not one that I tested I had worked on a few others that i did so I was mistaken. the do you have a way to check what information is being posted, besides having to echo all the variables? like effetch http sniffer, tamperdata or something like that? I am wondering if all your variables are correct and not empty also add a or die(mysql_error()); to the end of the inserts so we can besure there is no problem there

Sponsor our Newsletter | Privacy Policy | Terms of Service