0

So I've been wrestling with this issue all day. I can't seem to post anything into my table and I'm not sure why.

I've got a form built that has all the values that are being transferred for the _POST. Any pointers would be great.

elseif ($request == 'POST') {

include 'header_post.php'; include 'topmain.php';

$fname = $_POST['fname'];
$lname = $_POST['lname'];
$dlnum = $_POST['dlnum'];
$dob = $_POST['dob'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];
$height = $_POST['height'];
$weight = $_POST['weight'];
$hair = $_POST['hair'];
$eyes = $_POST['eyes'];
$ethnicity = $_POST['ethnicity'];
}
$query3 = "insert into ".$db_prefix."customer_det (fname, lname, dlnum, dob, address, city, state, zip, phone, height, weight, hair, eyes, ethnicity) 
           values ('".$fname."', '".$lname."', '".$dlnum."', '".$dob."', '".$address."', '".$city."', '".$state."', 
           '".$zip."', '".$phone."', '".$height."', '".$weight."', '".$hair."', '".$eyes."', '".$ethnicity."')";
3
  • Have you established a connection to the database? Commented Oct 18, 2012 at 17:24
  • Becides the obvious sql injection, you're never executing the query anywhere. Commented Oct 18, 2012 at 17:25
  • Please let me know what site you're doing this for so I can practice up on my SQL injection skills. This is the most dangerous query I've seen in a while. Commented Oct 18, 2012 at 17:26

4 Answers 4

2

Using MySQL connection in PHP:

mysql_connect('DB_HOST','DB_USER','DB_PASS');
@mysql_select_db('DB_NAME');
$query='insert into ...';
mysql_query($query);

But your query is prone to SQL injections, and you are advised to use MySQLi extension.

    $mysql = new mysqli('DB_HOST', 'DB_USER', 'DB_PASS', 'DB_NAME');
    $query = 'insert into customer_det (
        fname,
        lname,
        dlnum,
        .......
    ) values (?,?,?,....,?)';
    $statement = $mysql->prepare($query);
    $statement->bind_param('sss...', //How many ever fields are there, those many sssss. For Integer use i. s is for string fields. Example ssssisssi....
        $fname,
        $lname,
        $dlnum,
        .....);
    $statement->execute();
    $statement->close();
    $mysql->close();
Sign up to request clarification or add additional context in comments.

Comments

0

you should use mysql_query :

$query3 = mysql_query("insert..");

you should also add this before $fname = $_POST['fname']; to prevent the query at page load :

if(isset($_POST['fname'])){

 $fname = $_POST['fname'];
 ....
 $query3 = mysql_query("insert..");
}

1 Comment

I really appreciate it mgraph.
0

You are not performing query

use $result=mysql_query($query3);

2 Comments

Any idea why it's performing the query as soon as the page loads and not when it's submitted?
use if(isset($_POST['hair'])){mysql_query($QUERY)}
0

There must be a query called. use mysql_query();

As mentioned above, any variable using user inputted text that is stored in a database should be contained within the mysql_real_escape_string() to prevent SQL Injection.

2 Comments

As i mysql_real_escape_string($variable) or mysql_real_escape_string($_POST['value'])
Both would work, but since the variables are already being used in the query, go with the first one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.