php - How do i autogenerate an ID in a form? -
<html> <head> <title>the test</title> </head> <body> <?php require 'dbconn.php'; $db = @odbc_connect ($dbconn, '', ''); if (array_key_exists('id', $_post)) { $sql = "insert landlords (id, landlord, address, contact) values ('$_post[id]','$_post[land]', '$_post[add]', '$_post[con]')"; print '<h2>landlord ' .$_post['id'] .' added</h2>'; print '<p>return @ <a href=admin.htm>admin page</a>'; odbc_exec ($db, $sql); } else { ?> <form action="addlandlord.php" method="post"> <p>create new landlord id:<br><input type="text" name="id" size="30"> <p>enter landlord's name:<br><input type="text" name="land" size="30"> <p>enter landlord's address:<br><textarea name="con" cols="50" rows="10" </textarea> <p>enter landlord's contact details:<br><textarea name="con" cols="50" rows="10"> </textarea> <p>and click here <input type=submit value="submit"> </form> <?php } ?> </body> </html>
hi, firstly when using code have create id - need in autogenerating these values , entering them database. database automatically generates them, don't know how change php accomodate this.
secondly, how add "you entered wrong values must , so" pop when entering wrong values?
thanks guys!
if database table has id column set auto increment, don't want try , set value. that's auto increment does: creates id automatically unique. moreover, if database isn't set auto increment id, change it. it's terrible idea ask user create id. forget fact require lot of unnecessary code try , make sure unique , potentially frustrating , forth user. ick!
- delete input id in html.
- remove array_key_exists if statement, can check if values set isset btw.
set post values in variables (if don't you'll need add quotes on $_post["value"] array key , need escape quotes in statement or have enclose each in curly braces), change sql statement this:
$sql = "insert landlords (landlord, address, contact) values ($landlord, $add, $con)";
the database handle id auto increment virtue of creating new record.
for validation, need search tutorials. there tons of them php.
edit: here's sample page checking make sure values aren't empty. that's does. said there many many tutorials validation , should use tutorial learn many ways can accomplished.
also, don't kind of database you're connecting to, i'm guessing connection string , sql statement correct. further, assumes database id column indeed set autogenerate , id (auto increment or identity or whatever database supports).
there comments in code provide hints what's going on, should self-explanatory:
<html> <head> <title>the test</title> <style> .error { color: red; }; </style> </head> <body> <?php require 'dbconn.php'; $db = @odbc_connect ($dbconn, '', ''); $display = "form"; $landlord = ""; $landlorderror = ""; $address = ""; $addresserror = ""; $contact = ""; $contacterror = ""; if($_server['request_method'] == 'post') { if (empty($_post["landlord"])) { $landlorderror = "landlord name required"; } else { $landlord = cleanup($_post["landlord"]); } if (empty($_post["address"])) { $addresserror = "address required"; } else { $address = cleanup($_post["address"]); } if (empty($_post["contact"])) { $contacterror = "contact info required"; } else { $contact = cleanup($_post["contact"]); } if ($landlorderror === "" && $addresserror === "" && $contacterror === "") { $sql = "insert landlords (landlord, address, contact) values ('$landlord', '$address', '$contact')"; odbc_exec ($db, $sql); //not sure db you're connecting to, if supports odbc_num_rows() //you can check here if insert resulted in record being created //by checking if returns 1 , setting $display value inside if statement $display = "successmsg"; } } //this little sanitizing if can use pdo, better function cleanup($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); //b/c you're displaying data in same form return $data; } //so, going display success message if display variable has been set other form. //that happens if there no empty fields , db insert run if ($display != 'form') { print '<h2>landlord ' .$landlord .' added</h2>'; print '<p>return @ <a href=admin.htm>admin page</a>'; } else { //the form action set , each of input values set value entered //if form attempted submitted. if error exists, displayed after input label. ?> <form action="<?php echo htmlspecialchars($_server["php_self"]);?>" method="post"> <p>enter landlord's name:<span class="error">* <?php echo $landlorderror;?></span><br><input type="text" name="landlord" size="30" value="<?php echo $landlord;?>"> <p>enter landlord's address:<span class="error">* <?php echo $addresserror;?></span><br><textarea name="address" cols="50" rows="10"><?php echo $address;?> </textarea> <p>enter landlord's contact details:<span class="error">* <?php echo $contacterror;?></span><br><textarea name="contact" cols="50" rows="10"><?php echo $contact;?> </textarea> <p>and click here <input type=submit value="submit"> </form> <?php } ?> </body> </html>
Comments
Post a Comment