Reputation: 3111
I'm trying to insert data into my MySQL table, but with no luck. My code looks like:
$mysqli = mysqli_connect("localhost","login info");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
if (isset($_POST['submit'])) {
$number = $_POST['number'];
$road = $_POST['road'];
$postcode=$_POST['postcode'];
$price=$_POST['price'];
$type=$_POST['type'];
$bedrooms=$_POST['bedrooms'];
$agent=$_POST['agent'];
$featured=$_POST['featured'];
$keywords=$_POST['keywords'];
$mysqli->query("
INSERT INTO listings-rent
(number, road, postcode, price, type, bedrooms, agent, featured, keywords)
VALUES
('$number','$road','$postcode','$price','$type','$bedrooms','$agent','$featured','$keywords')");
}
The connection is fine, returns no errors.
Upvotes: 1
Views: 11687
Reputation: 7
try this instead:
$mysqli = mysqli_connect("localhost", "your_db_username" , "your_db_password" , "database_name")
for local server, usually the username is root and password is null. So in that case copy paste this:
$mysqli = mysqli_connect("localhost", "root" , "" , "login info")
Upvotes: 0
Reputation: 1213
You table name has a -
which is not a supported character for an unquoted table name in MySQL as stated in this documentation. Try this:
$mysqli->query("INSERT INTO `listings-rent` (number, road, postcode, price, type, bedrooms, agent, featured, keywords) VALUES ('$number','$road','$postcode','$price','$type','$bedrooms','$agent','$featured','$keywords')");
To view the error you can use echo $mysqli->error;
mentioned here.
Upvotes: 4
Reputation: 263723
Your table name should be enclosed with backtick since it contains a non-alphanumeric character.
INSERT INTO `listings-rent` (...) VALUES (...)
and also please do parameterize the values.
Upvotes: 6
Reputation: 9913
probably the `` are missing in your table name
i will suggest :
if (TRUE == $mysqli->query("INSERT INTO `listings-rent` (number, road, postcode, price, type, bedrooms, agent, featured, keywords) VALUES ('$number','$road','$postcode','$price','$type','$bedrooms','$agent','$featured','$keywords')"))
echo "its working!"
else
echo $mysqli->error;
this way you will see the problem
other option i would suggest it to print the query that fails and insert it manually using phpmyadmin and check why it isnt working
Upvotes: 1