Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jun 2009
    Posts
    36
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Echo id field data

    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?



    Code:
    <?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

  • #2
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    PHP Code:

    <?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)

    ?>

  • Users who have thanked Phil Jackson for this post:

    BigDaddy13 (08-27-2009)

  • #3
    New Coder
    Join Date
    Jun 2009
    Posts
    36
    Thanks
    9
    Thanked 0 Times in 0 Posts
    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

  • #4
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    yes sorry, 2 mins, i forgot about you deleting data..

  • Users who have thanked Phil Jackson for this post:

    BigDaddy13 (08-27-2009)

  • #5
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    PHP Code:
    [PHP]

    <?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)

    ?>
    [/PHP]

  • Users who have thanked Phil Jackson for this post:

    BigDaddy13 (08-27-2009)

  • #6
    New Coder
    Join Date
    Jun 2009
    Posts
    36
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Thank you!!! That works perfectly!!!


    Larry

  • #7
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    no worries


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •