mysql to mysqli query not working

[Update]
Sorry guys, after posting this I noticed in the thread the $_POST was highlighted as being out because of the ', didn’t notice it on NotePad++. I’ve now fixed it by removing the ’ and its working.

Since I am here can I ask if anyone can recommend a good book, or some good tutorials for a absolute beginner for mySQLi. I keep stumbling upon old methods of doing things rather than MySQLi.

Thanks all.

First of all hello everyone, this is my first post on the site.

I am looking to learn PHP, so started off with buying a PHP book which was working and understanding well. As soon as it jumped into PHP in MySQLi I lost any sense, if anything confused me even more, syntax’s are different, then to mix it up there is a procedural way and PDO way both with different way of doing things. I’ve spent several hours on reading why some people like PDO and why some people don’t, could not find any PHP specifically teaching MySQLi, so I am hoping you can help me on this.

Below is a form I was trying to build, self practice from the book. Now whilst I could get the old version to work, I can’t get it to work in MySQLi.

When I enter $_POST[‘user’] etc I get an error and it doesn’t work, but if I enter a string ‘dave’ it works.

Can you please explain why $_POST[‘user’] worked in the old version but not in MySQLi? What should it be?

[php]

<?php include "connect.php"; $con=mysqli_connect("$dbserver","$dbuser","$dbpass","$db"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } // Perform queries if ( isset($_POST['submit']) ){ mysqli_query($con,"INSERT INTO members (name,password,email) VALUES ('$_POST['name']','$_POST['name']','$_POST['name']')"); }; mysqli_close($con); ?> Username:
Password:
Email:

[/php]

Don’t know of any books. I have some, but I rarely use them. The best way for me was to just look up what I needed and with a lot of help from forums like here.

As for the quotes, you can’t use like quotes in the same area, like what you noticed with the post. You can use ‘$_POST[“var”]’ or ‘$_POST[var]’, or ‘".$_POST[‘var’].’". The last one is normally used if you need to modify it, like changing the date format, upper/lower case, addslashes(), etc. If you do like what you had, you’d get wrong syntax errors when the query ran.

You also don’t need quotes around the variables in the connection line, they’re already strings.

Also don’t need the ; after the } (line 21 in here), that will through an unexpected ; error message.

This is not valid. If you have error reporting turned on, PHP will give you an error.

Sponsor our Newsletter | Privacy Policy | Terms of Service