Jump to content

Need help with a script


Avenging_Flame

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="[email protected]" type="email">
                    </p>
                    <p>
                         <input value="Submit" type="submit">
                    </p>
                </form>
            </article>
    </section>

 

Link to comment
https://forums.phpfreaks.com/topic/276952-need-help-with-a-script/
Share on other sites

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.