I'm trying to create a basic script to collect names and e-mail addresses from a form. It seems to work except that the records it adds to the table in mySQL are blank (i.e. the record is successfully added, but both fields are blank instead of containing the name and e-mail address).
Any insights would be much appreciated. Thanks!
<form action="/mailinglist.php">
<div style="color:white" style="background-color:#be0000">
<center>
<p><b>WANT UPDATES? SUBSCRIBE TO THE MAILING LIST!</b></p>
<br>
<input type="text" placeholder="NAME" name="name" required>
<br>
<input type="text" placeholder="E-MAIL ADDRESS" name="email" required>
<br>
<input type="submit" value="SUBSCRIBE">
</center>
</div>
</form>
<?php
$user = "username";
$password = "password";
$host = "localhost";
$dbase = "database";
$table = "email";
$name= $_POST['name'];
$email= $_POST['email'];
// Connection to DBase
$dbc= mysqli_connect($host,$user,$password, $dbase)
or die("Unable to select database");
$query= "INSERT INTO $table ". "VALUES ('$name', '$email')";
mysqli_query ($dbc, $query)
or die ("Error querying database");
echo 'You have been successfully added.' . '<br>';
mysqli_close($dbc);
?>
CodePudding user response:
You are not specifying any columns to add your data. See the first row in the code
INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)
CodePudding user response:
Please add method type in form tag.
1)<form action="/mailinglist.php" metod='post'>
Query should be
2)$query= "INSERT INTO table_name ('name','email') VALUES ("'.$name.'", "'.$email.'")";