Jump to content

Recommended Posts

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 connection
if (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

Link to comment
https://forums.phpfreaks.com/topic/288337-php-code-help/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/288337-php-code-help/#findComment-1478735
Share on other sites

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 by ginerjm
Link to comment
https://forums.phpfreaks.com/topic/288337-php-code-help/#findComment-1478747
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.