Jump to content

Need help with a script


Avenging_Flame
Go to solution Solved by DavidAM,

Recommended Posts

For the life of me I cannot figure out what I am doing wrong here.

 

<?php //data.php
$con = mysql_connect("localhost","avenging_record","passwordwithheld","avenging_records");
if (!$con)
   {
   die('Could not connect: '. mysql_error());
   }
mysql_select_db("avenging_records");

// Get values from form
$Fname     = $_POST['first_name'];
$Lname     = $_POST['last_name'];
$street1   = $_POST['street_address1'];
$street2   = $_POST['street_address2'];
$city      = $_POST['city'];
$state     = $_POST['state_province'];
$zip       = $_POST['zip_postal'];
$gender    = $_POST['gender'];
$email     = $_POST['email'];

// Insert data into mysql
$order="INSERT INTO customer (first_name, last_name, street_address1, street_address2, city, state_province, zip_postal, gender, email)
VALUES ('$Fname','$Lname','$street1','$street2','$city','$state','$zip','$gender','$email')";
$result = mysql_query($order);

//if successfully insert data into database, displays message "Successful".
if($result){
echo "Thank you, your customer information has been entered";
}
else {
echo "ERROR";
}

//close mysql
mysql_close();
?>

 

This program will not work no matter what. I have tried changing many things about it but I just can't seem to get it to work.It says error no matter what. Any help would be much appreciated. For reference below is the form that it is getting data from

 

<section id="top area">
          <article class="box-right">
               <form action="data.php" method="post">
                    <p>
                         <label>First Name:</label>
                         <input name="first_name" required="required" placeholder="Jane" type="text">
                    </p>
                    <p>
                         <label>Last Name:</label>
                         <input name="last_name" required="required" placeholder="Doe" type="text">
                    </p>
                    <p>
                         <label>Street Address 1:</label>
                         <input name="street_address1" required="required" placeholder="street address" type="text">
                    </p>
                    <p>
                         <label>Street Address 2:</label>
                         <input name="street_address2" type="text">
                    </p>
                    <p>
                         <label>City:</label>
                         <input name="city" required="required" placeholder="City" type="text">
                    </p>
                    <p>
                         <label>State/Province:</label>
                         <input name="state_province" required="required" placeholder="State/Province" type="text">
                    </p>
                    <p>
                         <label>Zip/Postal Code:</label>
                         <input name="zip_postal" required="required" placeholder="Zip/Postal Code" type="text">
                    </p>
                    <p>
                         <label>Gender:</label>
                         <input type="radio" name="gender" value="male" checked="checked" /><label>male</label>
                         <input type="radio" name="gender" value="female" /><label>female</label>
                    </p>
                    <p>
                         <label>Email Address:</label>
                         <input name="email" required="required" placeholder="name@email.com" type="email">
                    </p>
                    <p>
                         <input value="Submit" type="submit">
                    </p>
                </form>
            </article>
    </section>

 

Edited by Avenging_Flame
Link to comment
Share on other sites

  • Solution

Echo mysql_error when $result is FALSE. It will print the error from the database server.

 

BTW:

 

You need to sanitize ALL user input. See mysql_real_escape_string

 

You never checked to see if the form was actually posted. If someone goes to data.php in the browser, it will (attempt to) insert a row of empty values into the database.

 

Turn on error reporting, so you can see any PHP errors (warnings, etc)

 

The mysql extension has been deprecated. You should switch to mysqli (or PDO) for any new development.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.