/    Sign up×
Community /Pin to ProfileBookmark

need page to INSERT new ID into two tables

I got my PHP/MYSQL start about a year ago with K. Yank book “Build Database Driven Website”, so I am [U]still[/U] [U]new[/U] at coding PHP/MYSQL.

My current project is building a real estate website – almost done. The CMS has form pages that ADD / EDIT / DELETE residential records. The ADD page creates a new residential record in the database; and the EDIT and DELETE pages manage each residential record.

Each residential record has four pictures associated with that record. Adding pictures is done in the EDIT page. Under my design, the admin must first create a record using the ADD page.

——–ISSUE———

The ADD page inserts a record into the RESIDENTIAL TABLE; RES_ID is the primary key. I also need the ADD page to INSERT the newly created RES_ID into the IMAGES TABLE field IMAGE_RES_ID.

[code=php]
<?php

if ($submit): // add new CONTENT from form below

$sqlinsert = “INSERT INTO residential SET
res_order=’$resorder’,
res_head=’$reshead’,
res_description=’$resdescription’,
res_price=’$resprice'”;
if (@mysql_query($sqlinsert)) {

echo(“<p>New Residential property added!</p>”);
} else {
echo(“<p>Error adding new Residential property: ” . mysql_error() . “</p>”);
}

?>
[/code]

My research and thoughts make this code (or something like it) look like a solution but my testing proves me wrong on its placement in the code and it syntax.

$id = mysql_insert_id();
$imginsert = “INSERT INTO image SET image_res_id=’$id'”;

Any one see where I went off the road? Thanks in advance.

Greg

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@gregoriousauthorJul 20.2007 — okay, found the solution [for this coding style]. I inserted this code before the end of the closing PHP tag ?> and I got the newly created ID primary key from the RESIDENTIAL table to be INSERTED into the IMAGE table in the image_res_id field.


[code=php]$id = mysql_insert_id();
$imginsert = "INSERT IGNORE INTO image SET image_res_id=$id";
if (@mysql_query($imginsert)) {

echo("<p>New Residential ID added to Image table!</p>");
} else {
echo("<p>Error adding Residential ID into Image table! " . mysql_error() . "</p>");
}


?>[/code]
Copy linkTweet thisAlerts:
@gregoriousauthorJul 20.2007 — While doing my research on the issue of inserting data into more than one table with the same form, I came across another solution that was written in a different syntax of code.

I was wondering why it differs from the syntax I use.

My FORM syntax:

[code=php]<?php
if (isset($_POST['submit'])): // add new CONTENT from form below

$sqlinsert = "INSERT INTO residential SET

res_order='$resorder',

res_head='$reshead',

res_description='$resdescription',

res_price='$resprice'
if (@mysql_query($sqlinsert)) {

echo("<p>New Residential property added!</p>");
} else {
echo("<p>Error adding new Residential property: " . mysql_error() . "</p>");
}
?> [/code]


and other FORM syntax:

[code=php]<?php

if(isset($_POST['artists'])):


$artists = $_POST['artists'];
$bio = $_POST['bio'];
$gigdate = $_POST['gigdate'];
$gigtime = $_POST['gigtime'];
$price = $_POST['price'];
$venue_name = $_POST['venue_name'];
$venue_city = $_POST['venue_city'];
$venue_address = $_POST['venue_address'];
$venue_postcode = $_POST['venue_postcode'];
$venue_tel = $_POST['venue_tel'];

$sql = mysql_query("INSERT INTO artists ( artists , bio )
VALUES ('$artists', '$bio')");

$sql2 = mysql_query("INSERT INTO listings ( gigdate , gigtime , price )
VALUES ('$gigdate', '$gigtime', '$price')");

$sql3 = mysql_query("INSERT INTO venue ( venue_name, venue_city, venue_address, venue_postcode, venue_tel )
VALUES ('$venue_name', '$venue_city', '$venue_address', '$venue_postcode', '$venue_tel')");

if(@mysql_query($sql and $sql2 and $sql3))
{
echo '<p>New Listing!</p>';
}
else
{
echo '<p>Error:' . mysql_error() . '</p>';
}
?> [/code]
×

Success!

Help @gregorious spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...