Reputation: 75
I am trying to make a registration form. I can connect successfully to mySQL database but i can't insert anything to my table named "hackers" from my html form. Here is my form and my php script.
I also tried to insert data manually in $sql to see if the problem is in the form, but $sql doesn't add data either.
Your help is much much appreciated!!!!!!!
<?php
include"config.php";
$insert = 'INSERT INTO hackers (name,email, school, grad-year) VALUES('$_POST['name']','$_POST['email']','$_POST['school']','$_POST['gradYear']')';
$sql= 'INSERT INTO mydatabase_name.hackers (name,email, school, grad-year) VALUES ('text', 'text', 'text', 'text')';
mysql_query($insert);
mysql_query($sql);
?>
My form:
<form method = "post" action ="process.php" >
<table id ="registration_form">
<tr>
<td>name</td>
<td><input type= "text" name="name"/></td>
</tr>
<tr>
<td>email</td>
<td><input type= "text" name="email"/></td>
</tr>
<tr>
<td>school</td>
<td><input type= "text" name="school"/></td>
</tr>
<tr>
<td>graduation year</td>
<td><input type= "text" name="gradYear"/></td>
</tr>
<tr>
<td><input type= "submit" name="submit" value ="Submit"/></td>
</tr>
</table>
</form>
Upvotes: 2
Views: 5060
Reputation: 21575
You need to concatenate with .
, use double quotes on the outside and single values for value adding. For example:
"INSERT INTO hackers (name,email, school, grad-year) VALUES('".$_POST['name']."','".$_POST['email']."','".$_POST['school']."','".$_POST['gradYear']."')"
Although I want to note that this isn't a good idea as you are prone to SQL-injections. You want to escape or prepare the query first before you execute it.
For your manually entered string you have a similar problem. In this case an easy fix is to have double quotes on the outside of the string and single quotes on the inside, the problem is that this ES ('te
closes the string for the query here: $sql= 'INS
.
"INSERT INTO mydatabase_name.hackers (name,email, school, grad-year) VALUES ('text', 'text', 'text', 'text')"
Upvotes: 2
Reputation: 11
$name = $_POST['name'];
$school= $_POST['school'];
$email = $_POST['email'];
$grad_year = $_POST['gradYear']; //watch out for mysql injection here
$insert = "INSERT INTO hackers (name,email, school, grad-year) VALUES('$name','$email','$school','$grad_year')";
$sql= "INSERT INTO mydatabase_name.hackers (name,email, school, grad-year) VALUES ('text', 'text', 'text', 'text')";
This should work. Look out for the proper quotes.
Upvotes: 1
Reputation: 5913
Your problem is the quotes. The following example is correct, adapt to yours.
$sql = "INSERT INTO mydatabase_name.hackers (name, email)
VALUES ('text', '$someVariable')";
Upvotes: 0