By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,664 Members | 1,612 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,664 IT Pros & Developers. It's quick & easy.

two insert queries in my code

P: 11
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

[PHP]<?php


$self = $_SERVER['PHP_SELF'];
$ISSN = $_POST['ISSN'];
$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">
</form>

<?php

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" );
}
else
{
echo("Record not added");
}
}

?>
[/PHP]


hope any one can help
Feb 11 '07 #1
Share this Question
Share on Google+
4 Replies


P: 99
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.

[PHP]
$sql = "insert into journal ( ISSN, Title, Volume, Number ) values ( $ISSN, \"$Title\", \"$Volume\", \"$Number\" );";
$sql .= "insert into journal1 (ID, ISSN) values ($ID,\"$ISSN\")"
[/PHP]
Feb 11 '07 #2

ronverdonk
Expert 2.5K+
P: 4,258
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());
else
echo "Journal1 record added:$ISSN";
}

?>[/php]
Ronald :cool:
Feb 11 '07 #3

P: 11
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

P: 99
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

[PHP]
function multipleDelete($queries){
$affectedRows = array();
foreach($queries as $query){
$result = mysql_query($query);
$affectedRows[] = mysql_affected_rows()
}
return $affectedRows;
}
[/PHP]
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.