hi there, i having problem in auto increment in my sql database. i've tried it but i didn't get error or any data into my database. here's the code that i've tried i actually wanted my ID to be M001 but the database couldn't allow me to have the varchar for auto increment please help! thanks in advance <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta content="en-us" http-equiv="Content-Language" /> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <title>Name</title> </head> <body> <?php if (isset($_POST['submit'])){ $con = mysql_connect("127.0.0.1","root","2614352") or trigger_error("Connection Failed: ".mysql_error($con), E_USER_ERROR); mysql_select_db("cbfsms", $con); $_POST = array_map('mysql_real_escape_string', $_POST); $sql="INSERT INTO member (null,MemName, MemAdd, MemContact, MemEmail) VALUES ('{$_POST[MemID]}','{$_POST['MemName']}','{$_POST['MemAdd']}','{$_POST['MemContact']}','{$_POST['MemEmail']}')"; if (mysql_query($sql,$con)){ echo "1 record added"; } else { trigger_error("Query Failed: ".mysql_error($con), E_USER_ERROR); } mysql_close($con); } ?> <form method="post"> Name: <input name="MemName" type="text" /><br /> <br /> Address: <input name="MemAdd" type="text" /><br /> <br /> Email: <input name="MemEmail" type="text" /><br /> <br /> Contact: <input name="MemContact" type="text" /><br /> <br /> <input name="clear" type="button" value="clear" /> <input name="submit" type="submit" value="submit" /> <input name="cancel" type="button" value="cancel" /></form> </body> </html>
I cant see why your first thing should be null... $sql="INSERT INTO member (null,MemName, MemAdd, MemContact, MemEmail) PHP: You still want it to come into the MemID i suspect. so do this: $sql="INSERT INTO member (MemID, MemName, MemAdd, MemContact, MemEmail) PHP: as far as i can see... you have not specified what $_POST['MemID'] is... so therefore... if you use an increment value in the db (as you wrote, it cant be a varchar - must be an init): $sql="INSERT INTO member (MemID ,MemName, MemAdd, MemContact, MemEmail) VALUES ('','{$_POST['MemName']}','{$_POST['MemAdd']}','{$_POST['MemContact']}','{$_POST['MemEmail']}')"; PHP: Note: the first value (that goes into MemID) is empty - meaning that it does not say what it is - this enables your db to have an auto increment number there. Unfortunately, you will not be able to have an M before. I cant see why that should be necessary, otherwise, when you output your MemID value you can always add an M before it - like this: echo 'M'.$row['MemID']; PHP: Good Luck!
i've tried the auto increment like what u had wrote but still can't... it inserted null into ID field and in the ID field, i used integer for auto increment but still can't it didn't write into the database nor show any error messages. Thanks for the guidances...
Your field should be INT, so give it an int with length variable say, 11. under extras, make it auto_increment (if youre in your phpmyadmin). You don't need to insert a value when you insert new records, it will insert it automatically, and make it 1+ the previous record.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta content="en-us" http-equiv="Content-Language" /> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <title>Name</title> </head> <body> <?php if (isset($_POST['submit'])){ $con = mysql_connect("127.0.0.1","root","2614352") or trigger_error("Connection Failed: ".mysql_error($con), E_USER_ERROR); mysql_select_db("cbfsms", $con); $_POST = array_map('trim', $_POST); if (empty($_POST['MemName'])){ echo "Enter Name..."; } elseif (empty($_POST['MemAdd'])){ echo "Enter Address..."; } elseif (empty($_POST['MemContact'])){ echo "Enter Contact..."; } elseif (empty($_POST['MemEmail'])){ echo "Enter Email..."; } else { $_POST = array_map('mysql_real_escape_string', $_POST); $sql="INSERT INTO member (MemName, MemAdd, MemContact, MemEmail) VALUES ('{$_POST['MemName']}','{$_POST['MemAdd']}','{$_POST['MemContact']}','{$_POST['MemEmail']}')"; if (mysql_query($sql,$con)){ echo "1 record added"; } else { trigger_error("Query Failed: ".mysql_error($con), E_USER_ERROR); } mysql_close($con); } } ?> <form method="post"> Name: <input name="MemName" type="text" /><br /> <br /> Address: <input name="MemAdd" type="text" /><br /> <br /> Email: <input name="MemEmail" type="text" /><br /> <br /> Contact: <input name="MemContact" type="text" /><br /> <br /> <input name="clear" type="button" value="clear" /> <input name="submit" type="submit" value="submit" /> <input name="cancel" type="button" value="cancel" /></form> </body> </html> PHP: