Insert data query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shab620
Forum Commoner
Posts: 48
Joined: Tue Jan 18, 2005 7:50 pm

Insert data query

Post by shab620 »

Hye I'm trying to take a value from a select query and put that value into an insert query. at the moment everything but the select query result is being inserted.

the code i'm using is as follows

My Inital form where the user inputs there selection

Code: Select all

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form name="form1" method="post" action="processorder.php">
  <p>
    <label>    </label>
    <br>
    <label>
    <input type="radio" name="RadioGroup1" value="1125">
    Beans
    
</label>
    <br>
    <label>
    <input type="radio" name="orderlist" value="12">
Tuna Mayo Bagutte</label>
    <br>
    <label>
    <input type="radio" name="orderlist" value="13">
Tuna Salad</label>
    <br>
    <input type="radio" name="orderlist" value="11">
    Special    <br>
    <input type="text" name="comments">
Comments  </p>
  <p>
    <input type="text" name="quantity">
  Quantity  </p>
  <p>
    <input type="text" name="username">
Username</p>
  <p>
    <input type="submit" name="Submit" value="Submit">
</p>
</form>
</body>
</html>
My processing script where the select and insert script is

Code: Select all

<?php
$item_id=$_POST&#1111;'orderlist'];
$comments=$_POST&#1111;'comments'];
$quantity=$_POST&#1111;'quantity'];
$user_name=$_POST&#1111;'username'];
$hostname="localhost"; 
$mysql_login="root"; 
$mysql_password="******"; 
$database="coachhouse"; 
$connect = mysql_connect("$hostname", "$mysql_login" , "$mysql_password");
$table_name = emp_order;

$result = mysql_query ("SELECT item_description from item where item_id = '$item_id'",$connect);


$query = ("INSERT INTO emp_order VALUES ('','$result','$quantity','$comments','$user_name')");
mysql_query($query);
mysql_select_db($database);
PRINT '<br><font size ="4" colour="blue">';
IF (mysql_query($query))
PRINT "<center>Order Has Been Placed</Center></font><BR><BR>";

mysql_close($connect);
?>
Can anyone advise me on where i'm going wrong. The problem is the Insert statement, wont display anything for $result.
Does anyone maybe have a better solution?
I'm new to php so please dont get too technical with me, lol
regards

Shab
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

first do the usual debugging yourself.... and use mysql_error.....

this way you will see that you are missing mysql_select_db for example......
shab620
Forum Commoner
Posts: 48
Joined: Tue Jan 18, 2005 7:50 pm

Post by shab620 »

Hye

Tried the error reporting and displayed a message about no database seleted, have ammened the code but have a new error now. The table now displays a value of 'Resource id #2' in the column which should be populated by the select query. and two records are inserted each time when i press submit. the rest of the fields are being populated correctly.
I am using a mysql database.

Can anyone help?

Code: Select all

<?php
$item_id=$_POST&#1111;'orderlist'];
$comments=$_POST&#1111;'comments'];
$quantity=$_POST&#1111;'quantity'];
$user_name=$_POST&#1111;'username'];
$hostname="localhost"; 
$mysql_login="root"; 
$mysql_password="cavalier1"; 
$database="coachhouse"; 
$connect = mysql_connect("$hostname", "$mysql_login" , "$mysql_password");
$table_name = emp_order;
mysql_select_db($database);
$result = mysql_query ("SELECT item_description from item where item_id = '$item_id'",$connect)
or die("Invalid query: " . mysql_error());


$query = ("INSERT INTO emp_order VALUES ('','$result','$quantity','$comments','$user_name')");
mysql_query($query);


PRINT '<br><font size ="4" colour="blue">';
IF (mysql_query($query))
PRINT "<center>Order Has Been Placed</Center></font><BR><BR>";

mysql_close($connect);
?>
Hope somebody can help
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

mysql_query only returns a resource identifier on successful run of a selection query, which is what you have.. you need to use one of the mysql_fetch_* functions to retrieve the value(s) found. Note: mysql_fetch_* functions return one (1) record at a time.
shab620
Forum Commoner
Posts: 48
Joined: Tue Jan 18, 2005 7:50 pm

Post by shab620 »

Oh, thank you for that, will try that and see what i get.

Shab
shab620
Forum Commoner
Posts: 48
Joined: Tue Jan 18, 2005 7:50 pm

Post by shab620 »

Hye tried the changes and now i get a value of 'Array' and two records are inserted into the table each time. had to comment out the error report as it would have parsing errors.
and if i use '$row = mysql_fetch_*($result); then i get the value 0.

The code is as follows

Code: Select all

<?php
$item_id=$_POST&#1111;'orderlist'];
$comments=$_POST&#1111;'comments'];
$quantity=$_POST&#1111;'quantity'];
$user_name=$_POST&#1111;'username'];
$hostname="localhost"; 
$mysql_login="root"; 
$mysql_password="cavalier1"; 
$database="coachhouse"; 
$connect = mysql_connect("$hostname", "$mysql_login" , "$mysql_password");
$table_name = emp_order;
mysql_select_db($database);
$result = mysql_query ("SELECT Item_Description from item where item_id = '$item_id'",$connect);
$row = mysql_fetch_row($result);
//or die("Invalid query: " . mysql_error());

$query = ("INSERT INTO emp_order VALUES ('','$row','$quantity','$comments','$user_name')");
mysql_query($query);


PRINT '<br><font size ="4" colour="blue">';
IF (mysql_query($query))
PRINT "<center>Order Has Been Placed</Center></font><BR><BR>";
PRINT $row;

mysql_close($connect);
?>
As i say i'm not the best at PHP so please take lightly if i'm making common mistakes
How else will i get better, lol
shab
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

remove the semicolon on the line before the commented out die(), it'll work again.

The double insert is from calling mysql_query($query) twice. Once after $query is set to the INSERT statement. And once at the 'if' following it. To know if the insert succeeded, capture the return from mysql_query().

Code: Select all

$table_name = emp_order;
emp_order here should be in quotes.


I hope that isn't your real password to mysql.. ;)
shab620
Forum Commoner
Posts: 48
Joined: Tue Jan 18, 2005 7:50 pm

Post by shab620 »

Hye I've sorted that problem and it works correctly now. You were right about the double record insert thing as well.

Thanks for your help
Much appreciated
:D :D
Regards

Shab
Post Reply