...

View Full Version : Echo id field data



BigDaddy13
08-27-2009, 09:57 PM
Ok. I have a site that uses
html for the form and php for the insert
to the mysql database.
It will echo the information that
it puts in the fields, but how
do I get it to echo an auto-increment field?




<?php
//Format date
$date = preg_replace("#(\d{2})/(\d{2})/(\d{4})#", "$3-$1-$2", $_POST['date']);

$user="******";
$password="******";
$database="*******";
$con = mysql_connect(localhost,$user,$password);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db($database, $con);

//Validation
$county = mysql_real_escape_string($_POST['county']);
$school = mysql_real_escape_string($_POST['school']);
$abs = mysql_real_escape_string($_POST['abs']);
$absf = mysql_real_escape_string($_POST['absf']);
$shf = mysql_real_escape_string($_POST['shf']);
$fabs = mysql_real_escape_string($_POST['fabs']);
$comment = mysql_real_escape_string($_POST['comment']);
$date = mysql_real_escape_string($date);

$sql="INSERT INTO school (county, school, abs, absf, shf, fabs, date, comment)
VALUES
('$county','$school','$abs','$absf','$shf','$fabs','$date','$comment')";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record for $county and $school on $date added.";
echo '<br>';
echo "$abs Student Absentees.";
echo '<br>';
echo "$absf Student Absentees with ILI Symptoms.";
echo '<br>';
echo "$shf Students Sent Home with ILI Symptoms.";
echo '<br>';
echo "$fabs Faculty and Staff Absentees.";
echo '<br>';
echo '<br>';
echo '<a target="_top" href="http://www.d4sas.org/sas/index.html">Click Here To Add Another Record</a>';

mysql_close($con)

?>

The id field is simply id in mysql.

What I am doing is, for example if someone makes a mistake
inputting data, I want it to echo the id so they can refer
to it when submitting the problem to someone.
They will be able to email someone and say "hey,
I am a big dummy and I added the wrong number, can
you fix record number 543?"

Hope this makes sense!


Larry

Phil Jackson
08-27-2009, 10:15 PM
<?php
//Format date
$date = preg_replace("#(\d{2})/(\d{2})/(\d{4})#", "$3-$1-$2", $_POST['date']);

$user="******";
$password="******";
$database="*******";
$con = mysql_connect(localhost,$user,$password);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db($database, $con);

//Validation
$county = mysql_real_escape_string($_POST['county']);
$school = mysql_real_escape_string($_POST['school']);
$abs = mysql_real_escape_string($_POST['abs']);
$absf = mysql_real_escape_string($_POST['absf']);
$shf = mysql_real_escape_string($_POST['shf']);
$fabs = mysql_real_escape_string($_POST['fabs']);
$comment = mysql_real_escape_string($_POST['comment']);
$date = mysql_real_escape_string($date);

$query = mysql_query("SELECT * FROM `school` order by id");
$num = (mysql_num_rows($query)+1);


$sql="INSERT INTO school (county, school, abs, absf, shf, fabs, date, comment)
VALUES
('$county','$school','$abs','$absf','$shf','$fabs','$date','$comment')";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}

echo "1 record for $county and $school on $date added.";
echo '<br>';
echo "$abs Student Absentees.";
echo '<br>';
echo "$num INSERT ID.";
echo '<br>';
echo "$absf Student Absentees with ILI Symptoms.";
echo '<br>';
echo "$shf Students Sent Home with ILI Symptoms.";
echo '<br>';
echo "$fabs Faculty and Staff Absentees.";
echo '<br>';
echo '<br>';
echo '<a target="_top" href="http://www.d4sas.org/sas/index.html">Click Here To Add Another Record</a>';

mysql_close($con)

?>

BigDaddy13
08-27-2009, 10:22 PM
Thank you for the response.
However, The id that it echos is 451
but the auto-increment from the mysql
database is 533.

Any thoughts?


Larry

Phil Jackson
08-27-2009, 10:24 PM
yes sorry, 2 mins, i forgot about you deleting data..

Phil Jackson
08-27-2009, 10:25 PM
<?php
//Format date
$date = preg_replace("#(\d{2})/(\d{2})/(\d{4})#", "$3-$1-$2", $_POST['date']);

$user="******";
$password="******";
$database="*******";
$con = mysql_connect(localhost,$user,$password);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db($database, $con);

//Validation
$county = mysql_real_escape_string($_POST['county']);
$school = mysql_real_escape_string($_POST['school']);
$abs = mysql_real_escape_string($_POST['abs']);
$absf = mysql_real_escape_string($_POST['absf']);
$shf = mysql_real_escape_string($_POST['shf']);
$fabs = mysql_real_escape_string($_POST['fabs']);
$comment = mysql_real_escape_string($_POST['comment']);
$date = mysql_real_escape_string($date);


$sql="INSERT INTO school (county, school, abs, absf, shf, fabs, date, comment)
VALUES
('$county','$school','$abs','$absf','$shf','$fabs','$date','$comment')";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
$query = mysql_query("SELECT * FROM `school` order by id DESC");
while($row = mysql_fetch_array($query))
{
$num = $row["id"];
break;
}

echo "1 record for $county and $school on $date added.";
echo '<br>';
echo "$abs Student Absentees.";
echo '<br>';
echo "$num INSERT ID.";
echo '<br>';
echo "$absf Student Absentees with ILI Symptoms.";
echo '<br>';
echo "$shf Students Sent Home with ILI Symptoms.";
echo '<br>';
echo "$fabs Faculty and Staff Absentees.";
echo '<br>';
echo '<br>';
echo '<a target="_top" href="http://www.d4sas.org/sas/index.html">Click Here To Add Another Record</a>';

mysql_close($con)

?>

BigDaddy13
08-27-2009, 10:41 PM
Thank you!!! That works perfectly!!!


Larry

Phil Jackson
08-27-2009, 10:46 PM
no worries



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum