简体   繁体   中英

Form inputs getting saved in MySQL database as '1'

I have the below code in PHP to test saving data from a simple web form into MySQL. The form has two basic boxes which accept text: input1 and input2. When I complete these and hit submit, there are no issues. However, when I access the database in MySQL I just see '1' where I should see the actual text I entered. At first I thought this acted like a boolean but both fields are set up to accept VARCHAR (and set up as null). The 1 appears whether I enter text in the form or not.

The very basic HTML form:

<form action="signupform.php" method="post" />
<p> Input 1: <input type="text" name="input1" /></p>
<p> Input 2: <input type="text" name="input2" /></p>
<input type="submit" value="Submit" />
</form>

Here's the PHP used:

$connection = mysqli_connect(DB_SERVER,DB_USER,DB_PASSWORD,DB_NAME);

if (!$connection) {
    die('Could not connect: ' . mysqli_connect_error());
}

$connection_selected = mysqli_select_db($connection, 'demo');

if (!$connection_selected) {
    die('Can\'t use ' . 'DB_NAME' . ': ' . mysqli_error($connection));
}

echo 'Connected successfully';

$value1 = isset($_POST['input1']);
$value2 = isset($_POST['input2']);

$sql = "INSERT INTO form1 (input1, input2) VALUES ('$value1', '$value2')";
if (!mysqli_query($connection, $sql)) {
    die('Error: ' . mysqli_error($connection));
}

mysqli_close($connection);

?>

Appreciate the help.

You're doing something wrong here

$value1 = isset($_POST['input1']);
$value2 = isset($_POST['input2']);

what do you do here is, you're saving the boolean value. to the variables $value1 & $value2 respectively. That is True = 1 & False = 0 and You're inserting it to the database. No wonder you have 0 and 1 in the database.

if(isset($_POST['input1']) && isset($_POST['input2'])){
   $value1 = $_POST['input1'];
   $value2 = $_POST['input2'];
}

this would do

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM