markharris1990 Posted May 8, 2014 Share Posted May 8, 2014 Hi im new to all this coding with php and mysql, im trying to link some textboxes on a sign up page of a html to a table i have in mysql i was wondering if anyone would be able to check this code for me to see if im going wrong somewhere because im getting a failed to connect to mysql error but im not sure if its my code thats wrong or one of my account details that is wrong thank you <?php$con=mysqli_connect("mysqlusername","password","database name","table name");// Check connectionif (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error();}// escape variables for security$title = mysqli_real_escape_string($con, $_POST['title']);$firstname = mysqli_real_escape_string($con, $_POST['firtname']);$surname = mysqli_real_escape_string($con, $_POST['surname']);$email = mysqli_real_escape_string($con, $_POST['email']);$password = mysqli_real_escape_string($con, $_POST['pasword']);$DOB = mysqli_real_escape_string($con, $_POST['dob']);$Add1 = mysqli_real_escape_string($con, $_POST['add1']);$add2 = mysqli_real_escape_string($con, $_POST['add2']);$city = mysqli_real_escape_string($con, $_POST['city']);$post = mysqli_real_escape_string($con, $_POST['post']);$sql="INSERT INTO members (FirstName, surname, email, password, dob, Add1, Add2, postcode, city)VALUES ('$title', '$firtname', '$surname', '$email', '$password', '$dob', '$add1', '$add2', '$city', '$post')";if (!mysqli_query($con,$sql)) { die('Error: ' . mysqli_error($con));}echo "1 record added";mysqli_close($con);?> so what the code is doing is telling it to log into to mysql account and transfer the content of each of the textboxes into the matching cell in the table Quote Link to comment https://forums.phpfreaks.com/topic/288337-php-code-help/ Share on other sites More sharing options...
maxxd Posted May 8, 2014 Share Posted May 8, 2014 Your mysqli_connect() parameters are incorrect. It's mysqli_connect({hostname},{username},{password},{databasename}); You're currently skipping the host and adding a table name. Quote Link to comment https://forums.phpfreaks.com/topic/288337-php-code-help/#findComment-1478727 Share on other sites More sharing options...
markharris1990 Posted May 8, 2014 Author Share Posted May 8, 2014 thats great thanks ive changed that so now my line of code says mysqli_connect({hostname},{username},{password},{databasename}); and its running the script but im getting a redirection to a yahoo search saying site cant be found is there any reason for this. sorry about the questions im new to all this and its a bit annoying that im so close to cracking it but theres something always stopping it working Quote Link to comment https://forums.phpfreaks.com/topic/288337-php-code-help/#findComment-1478735 Share on other sites More sharing options...
maxxd Posted May 8, 2014 Share Posted May 8, 2014 Are you developing locally or on a remote server? If you're local, you'll more than likely enter 'http://localhost/path/to/site' in the browser's location bar. If remote, make sure you're spelling the site address correctly. Quote Link to comment https://forums.phpfreaks.com/topic/288337-php-code-help/#findComment-1478737 Share on other sites More sharing options...
ginerjm Posted May 8, 2014 Share Posted May 8, 2014 $con=mysqli_connect("mysqlusername","password","database name","table name"); The above line is not correct. Refer to the manual to get the right syntax. Quote Link to comment https://forums.phpfreaks.com/topic/288337-php-code-help/#findComment-1478739 Share on other sites More sharing options...
markharris1990 Posted May 8, 2014 Author Share Posted May 8, 2014 thanks its on a remote server, the hosting site have their own php and mysql servers so im running from those Quote Link to comment https://forums.phpfreaks.com/topic/288337-php-code-help/#findComment-1478740 Share on other sites More sharing options...
maxxd Posted May 8, 2014 Share Posted May 8, 2014 If the address is spelled correctly and you're still getting redirected, the DNS hasn't resolved. Try accessing the site by IP instead. Quote Link to comment https://forums.phpfreaks.com/topic/288337-php-code-help/#findComment-1478742 Share on other sites More sharing options...
ginerjm Posted May 8, 2014 Share Posted May 8, 2014 If you have looked at the manual, you should realize that your arguments do not require parens around them. Quote Link to comment https://forums.phpfreaks.com/topic/288337-php-code-help/#findComment-1478744 Share on other sites More sharing options...
ginerjm Posted May 8, 2014 Share Posted May 8, 2014 (edited) Assuming that you figure out your connect problem, I'll point out the other errors you will encounter. You also should be sure that you have php error checking turned on to help you out. 1 - php is case sensitive - meaning that upper and lowercase used in var names is a pia. Stick to lowercase and avoid this problem. 2 - spelling errors in var names 3 - dates need to be in yyyymmdd format to place into a date field. Check out the strtotime and date functions as to how to do this. 4 - you only need to quote string vars in your query statement - nums (& dates) do not need them. 5 - you must be sure that your field names and values in the query statement are in the same order. Edited May 8, 2014 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/288337-php-code-help/#findComment-1478747 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.