php - How to input data into a mysql database from a form when the database already contains a column with data? -


i have user registration mysql database want users enter data using form. table has column i've prepopulated unique codes. idea each user gets unique code when register.

first, there better way handle setup have? second, if there isn't better way, how update 1 row of data each form submission?

here's i'm doing:

register form

<form action="confirm.php" method="post" onsubmit="return validate();"> <p>first name: <input type="text" name="firstname" /></p> <p>last name: <input type="text" name="lastname" /></p> <p>address: <input type="text" name="address" /></p> <p>city: <input type="text" name="city" /></p> <p>state: <input type="text" name="state" /></p> <p>zip code: <input type="text" name="zipcode" /></p> <p>phone number: <input type="text" name="phone" /></p> <p>email: <input type="text" name="email" /></p> <p><input type="submit" /></p> </form> 

confirm.php

<form action="register.php" method="post" onsubmit="return validate();"> <p>first name:  <input type="text" name="firstname" id="firstname" size="36"     value="<?php echo $_post['firstname']; ?>"> </p>  <p>last name:  <input type="text" name="lastname" id="lastname" size="36" value="<?php echo $_post['lastname']; ?>"> </p> <p>address:  <input type="text" name="address" id="address" size="36" value="<?php echo $_post['address']; ?>"> </p> <p>city:  <input type="text" name="city" id="city" size="36" value="<?php echo $_post['city']; ?>"> </p> <p>state:  <input type="text" name="state" id="state" size="36" value="<?php echo $_post['state']; ?>"> </p> <p>zip code:  <input type="text" name="zipcode" id="zipcode" size="36" value="<?php echo $_post['zipcode']; ?>"> </p> <p>phone number: <input type="text" name="phone" id="phone" size="36" value="<?php echo $_post['phone']; ?>"> </p> <p>email: <input type="text" name="email" id="email" size="36" value="<?php echo $_post['email']; ?>"> </p> <input type="hidden" name="redirect_values" value="true">  <input type="submit" name="confirm" value="confirm information"> <input type="button" name="return" value="change information"    onclick="javascript: window.history.back(-1)";> </form> 

register.php

<?php ob_start(); $url = 'registercomplete.php'; require("register_dbinfo.php"); $con=mysqli_connect($host,$username,$password,$database); if (mysqli_connect_errno()) {echo "failed connect mysql: ".mysqli_connect_error();} $firstname = mysqli_real_escape_string($con, $_post['firstname']); $lastname = mysqli_real_escape_string($con, $_post['lastname']); $address = mysqli_real_escape_string($con, $_post['address']); $city = mysqli_real_escape_string($con, $_post['city']); $state = mysqli_real_escape_string($con, $_post['state']); $zipcode = mysqli_real_escape_string($con, $_post['zipcode']); $phone = mysqli_real_escape_string($con, $_post['phone']); $email = mysqli_real_escape_string($con, $_post['email']); $sql="insert users (id, firstname, lastname, address, city, state, zipcode, phone, email)values ('','$firstname', '$lastname', '$address', '$city', '$state', '$zipcode', '$phone', '$email')"; if (!mysqli_query($con,$sql)) {die('error: ' . mysqli_error($con));} mysqli_close($con); while (ob_get_status())  {ob_end_clean();} header( "location: $url" ); ?> 

database columns:

id|promo|tickets|firstname|lastname|address|city|state|zipcode|phone|email

i'm not sure if i've understood question.

if id int, make attribute unique a_i (auto increment).

if need unique string promo, may want use hashing function (like sha-256) in order avoid string collisions.

in addition question suggest not concatenate escaped string since technique not prevent sql injection. should use prepared statements , stored procedures.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -