Jump to content

PHP/SQL question


insane

Recommended Posts

I've just started to dabble in PHP/SQL, and I've run into a problem.

 

 

 

 

 

 

 

I have a page set up so that you can enter your name and age, and it will enter these into an SQL database.

 

 

 

 

 

 

 

The description of the table is as follows:

 

 

 

 

 

 

 

sqlen6.png

 

 

 

 

 

 

 

Now, sometimes when I enter data, it inputs it into the table in a weird table:

 

 

 

 

 

 

 

sql1fg1.png

 

 

 

(note the 50,49,48 in the id field).

 

 

 

 

 

 

 

My input script is as follows:

 

 

 

 

 

 

 

<?php







if (isset($_POST['name'])) {







$name = $_POST['name'];



$age = $_POST['age'];



$booleanage = validAge($age);



if (!$booleanage) {



	exit('
invalid age entered.');



}







$sql = "INSERT INTO table_name (name, age) VALUES ('$name', $age)";



if (@mysql_query($sql)) { echo '
name entered into database';}



else { echo '
 error entering name & age into database: ' . mysql_error() . ''; }



}



?>

 

 

 

 

 

 

 

Any thoughts?

summerpngwy6.jpg
Link to comment
Share on other sites

I have little experience using mySQL, but have you been deleting other records as you add? The auto increment won't reuse IDs are far as I know. It never did when I was playing around with it anyway. :?

 

 

 

 

 

 

 

Another side note, the mySQL query browser is so much nicer than the command line. :-$

Link to comment
Share on other sites

What is the SQL code you used to make the tables? There could be an error with that.

 

 

 

 

 

 

 

It's in the php script:

 

 

 

 

 

 

 

$sql = "INSERT INTO table_name (name, age) VALUES ('$name', $age)"; 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

but have you been deleting other records as you add? The auto increment won't reuse IDs are far as I know.

 

 

 

 

 

 

 

Yes I have been deleting as I add but I don't see it as relevant. I added the record with id 48 first, then 49, then 50. But they are ordered 50, 49, and 48 in the table.

summerpngwy6.jpg
Link to comment
Share on other sites

Data in an sql table is not necessarily put in the order you placed it in. That's why there is an ORDER BY statement.

 

 

 

 

 

 

 

SELECT * FROM table ORDER BY columname theorderyouwant

 

 

 

 

 

 

 

theorderyouwant = ASC(ascending) or DESC(descending)

Link to comment
Share on other sites

Data in an sql table is not necessarily put in the order you placed it in. That's why there is an ORDER BY statement.

 

 

 

 

 

 

 

SELECT * FROM table ORDER BY columname theorderyouwant

 

 

 

 

 

 

 

theorderyouwant = ASC(ascending) or DESC(descending)

 

 

 

 

 

 

 

That's it. Thank you very much :D

summerpngwy6.jpg
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.