0

This is definitely a beginner's question. There are two issues. The id in my MYSQL table (set to autoincrement) keeps going up, even though I delete rows from my table (I'm using phpmyadmin). As for the other issue, I can't seem to find a way to work with the row most recently entered by the user. The code echos all existing rows from MYSQL. I've bolded the most pertinent section of code.

<?php
//establish connection to mysql
$con = mysql_connect("localhost","root","");
if (!$con)
{
  die('Could not connect: ' . mysql_error());
}

/*retrieve user input from input form
and initialize variables */  
$Word1 = $_POST["Word1"];
$Word2 = $_POST["Word2"];
$Word3 = $_POST["Word3"];
$Word4 = $_POST["Word4"];
$Word5 = $_POST["Word5"];

//select db
mysql_select_db("madlibs", $con);  

//insert user input for word 1
$sql = "INSERT INTO test (Word1, Word2, Word3, Word4, Word5)
VALUES
('$Word1', '$Word2', '$Word3', '$Word4', '$Word5')";
if(!mysql_query($sql,$con))
{
  die('Error: ' . mysql_error());
}

$result = mysql_query ("SELECT * FROM test");  

/* take note here */
while($row = mysql_fetch_array($result))
{
  echo $row['Word1'] . " " . $row['Word2'] . " " . $row['Word3'] . " " . 
       $row['Word4'] . " " . $row['Word5'] . " " . $row['id'];
  echo "<br />";
} /* take note here */
mysql_close($con);
?>
1
  • can you put the whole code in between Commented Dec 5, 2010 at 2:48

2 Answers 2

2
$result = mysql_query ("SELECT * FROM test order by id desc limit 1");

As for your id question...that's how ids work. They don't fill in gaps.

On a side note: Never ever put user submitted data directly into the query string. Always escape them with mysql_real_escape_string().

Sign up to request clarification or add additional context in comments.

Comments

0
SELECT * FROM test ORDER BY Id DESC LIMIT 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.