467,871 Members | 1,708 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,871 developers. It's quick & easy.

two insert queries in my code

Hi everyone

i'm trying to put 2 sql queries in my code,

i have two tables journal, journal1. one query will insert values in the journal table and the othe query will insert value in the journal1 table


$self = $_SERVER['PHP_SELF'];
$ID = $_POST['ID'];
$Title = $_POST['Title'];
$Volume = $_POST['Volume'];
$Number = $_POST['Number'];

<form action="<?php echo( $self ); ?>" method="post">
ISSN: <input type="text" name="ISSN" size="3">
ID: <input type="text" name="ID" size="14">
Title: <input type="text" name="Title" size="14">
Volume: <input type="text" name="Volume" size="8">
Number: <input type="text" name="Number" size="8"><br>
<input type="submit" value="Submit">


if( $ISSN and $ID and $Title and $Volume and $Number)
$conn=@mysql_connect( "*****", "****", "*****" ) or die( "Err:Conn" );

#select the specified database
$rs = @mysql_select_db( "*****", $conn) or die( "Err:Db" );

#create the query
$sql = "insert into journal ( ISSN, Title, Volume, Number ) values ( $ISSN, \"$Title\", \"$Volume\", \"$Number\" )";
$sql2 = "insert into journal1 (ID, ISSN) values ($ID,\"$ISSN\")"
#execute query
$rs = mysql_query( $sql, $conn );

if( $rs )
echo( "Record added:$ISSN $Title $Volume $Number" );
echo("Record not added");


hope any one can help
Feb 11 '07 #1
  • viewed: 1418
4 Replies
add a semicolon after the statement in the first sql variable.

a semicolon tells mysql this is the end of the statement like it does in php.

$sql = "insert into journal ( ISSN, Title, Volume, Number ) values ( $ISSN, \"$Title\", \"$Volume\", \"$Number\" );";
$sql .= "insert into journal1 (ID, ISSN) values ($ID,\"$ISSN\")"
Feb 11 '07 #2
Expert 4TB
You could combine them but then it is difficult to pinpoint the error to the exact query. The following will do it for 2 separate statements[php]$sql = "insert into journal ( ISSN, Title, Volume, Number ) values ( $ISSN, \"$Title\", \"$Volume\", \"$Number\" )";
$sql2 = "insert into journal1 (ID, ISSN) values ($ID,\"$ISSN\")"
// ----------------
// execute query 1
// ----------------
if ($rs = mysql_query( $sql, $conn ))
or die("Record from query 1 not added: error = ".mysql_error());
else {
echo "Journal record added: $ISSN $Title $Volume $Number";
// ----------------
// execute query 2
// ----------------
if ($rs2 = mysql_query( $sql2, $conn ))
or die("Record from query 2 not added: error = ".mysql_error());
echo "Journal1 record added:$ISSN";

Ronald :cool:
Feb 11 '07 #3
thnx for both of you, but it seems that i wont be able to make it like this

after searching on the net i found out that php wont allow more than one query in one code.

yet i can create a function on my own to handle multiple query.

if any one knows a simple function to allow multiple qurey, i'll be greatful to see it.
Feb 15 '07 #4
if you want to create your own function i think it's best to split it up according to the sql statements; select, insert, replace, update,delete.

the delete could be something like

function multipleDelete($queries){
$affectedRows = array();
foreach($queries as $query){
$result = mysql_query($query);
$affectedRows[] = mysql_affected_rows()
return $affectedRows;
The insert,replace and update can be similar but for the select you need to return your result instead of the row number.

This is the function in the simpelest form you best add error checks and other security measures
Feb 15 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

16 posts views Thread by Philip Boonzaaier | last post: by
6 posts views Thread by Tom Allison | last post: by
7 posts views Thread by Kevin Lawrence | last post: by
4 posts views Thread by Treefrog | last post: by
3 posts views Thread by Bob Bedford | last post: by
11 posts views Thread by Eugene Anthony | last post: by
9 posts views Thread by David Eades | last post: by
2 posts views Thread by paulmitchell507 | last post: by
reply views Thread by jack112 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.