[Solved] Issue in storing data into database in php [closed]


  • As per your originally posted question which has been edited:

Firstly, you are using a hyphen for your sex-select column.

MySQL is thinking you want to do a math equation which translates to:

sex (minus) select.

Wrap it in backticks and missing the column for photo

(fname, lname, email, pass, phone, photo, `sex-select`, month,day,year)
                                   ^^^^^^ missing column

having used mysql_error() with mysql_query() would have signaled the error.

Also $sex-select that needs to be $sex_select do not use hyphens for variables also.

So change all of those instances to $sex_select in your VALUES also.

('$fname', '$lname', '$email', '$pass', '$phone', '$photo', '$sex_select', '$month','$day','$year')

I noticed both your DB and table are named crop.

$mysql_database = "crop";

and

mysql_query("INSERT INTO crop...

One of those may be incorrect. Make sure that the DB name is what it should be, along with the table. That just doesn’t look right to me, but I could be wrong. Only you know what they are called.

Plus mysql_close($con); this should be mysql_close($bd);
since you’re using $bd = mysql_connect

Add error reporting to the top of your file(s) which will help during production testing.

error_reporting(E_ALL);
ini_set('display_errors', 1);

which would have signaled that error also, which is something you are not doing, is checking for errors. This is important during development.


EDIT: for DB connection error checking:

Instead of:

$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "crop";
$prefix = "";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
mysql_select_db($mysql_database, $bd) or die("Could not select database");

Use the following to catch any errors, should there be any. Using die("Could not connect database") is not good enough.

If crop is not the database, MySQL will tell you.

<?php
$bd = mysql_connect('localhost', 'root', '');
if (!$bd) {
    die('Not connected : ' . mysql_error());
}

// make foo the current db
$db_selected = mysql_select_db('crop', $bd);
if (!$db_selected) {
    die ('Can\'t use foo : ' . mysql_error());
}

Same for your query:

$result = mysql_query("INSERT INTO crop(fname, lname, email, pass, phone, photo, `sex-select`, month,day,year)  
VALUES ('$fname', '$lname', '$email', '$pass', '$phone', '$photo', '$sex-select', '$month','$day','$year')");

if (!$result) {
    die('Invalid query: ' . mysql_error());
}

Plus, as per your HTML form:

<select name="sex-select" id="sex-select">

should be

<select name="sex_select" id="sex-select">

17

solved Issue in storing data into database in php [closed]