473,327 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

two insert queries in my code

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
4 1599
xwero
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
4,258 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());
else
echo "Journal1 record added:$ISSN";
}

?>[/php]
Ronald :cool:
Feb 11 '07 #3
Tara83
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
xwero
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

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

Similar topics

16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
6
by: Tom Allison | last post by:
I seemed to remember being able to do this but I can't find the docs. Can I run a sql query to insert new or update existing rows in one query? Otherwise I have to run a select query to see if...
7
by: Kevin Lawrence | last post by:
Hi all I want to do "INSERT INTO Table (Blob) Values('blobdataasstring')". ...rather than using the parameter driven method, is it possible? And if so what encoder do I use to convert the...
4
by: Treefrog | last post by:
Hi all, I came accross a little piece of code today that I thought was quite cute, but I'm not sure if it's any better than my usual method. The code ultimately needs to see if a unique...
3
by: Bob Bedford | last post by:
hello I'm looking for some functions or objects allowing to select-insert-update-delete from any table in a mysql database without the need to create a new query every time. Example: ...
11
by: Eugene Anthony | last post by:
The code bellow functions well when I attemp to delete a record. But when I attemp to insert a record I am getting the following error -2147217900:Syntax error in INSERT INTO statement. How do I...
9
by: David Eades | last post by:
Hi all Complete newbie here, so apologies if this is the wrong forum. I've been asked to use mysql and asp to make a simple bidding system (rather like a simple ebay), whereby users can use a...
5
by: DonnaL | last post by:
I'm using Access 2000, but this question likely pertains to any version of Access. Simply put, is there a programmatic way of inserting a new Query in whatever master system table stores these...
2
by: paulmitchell507 | last post by:
I think I am attempting a simple procedure but I just can't figure out the correct syntax. My asp (classic) page runs a SELECT query to obtain dates and ID's from 2 tables uSQL = "SELECT...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.