Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

is this code right to connect html form to my sql using php

<?php
     $con=mysqli_connect("localhost","root","****","inquiry_box");

// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// escape variables for security
$name = mysqli_real_escape_string($con, $_POST['name']);
$email = mysqli_real_escape_string($con, $_POST['email']);
$contact_no = mysqli_real_escape_string($con, $_POST['contact_no']);
$comment = mysqli_real_escape_string($con, $_POST['comment']);

$sql="INSERT INTO form (name, email, contact_no, comment)
VALUES ('$name', '$email', '$contact_no', '$comment')";

if (!mysqli_query($con,$sql)) {
  die('Error: ' . mysqli_error($con));
}
echo "1 record added";

mysqli_close($con);
?>

is this code right ?? i am trying to connect the html inquiry box with mysql and i am using this code but the probleme is it dont catch the data i type in html form but it rather show id number no data in the fields please help me thank you so much :D

Could you explain better what the issue is? Are you getting any error messages? If not, does anything save to the database?

The code looks very solid though -- did you write it???

Philip Cox
Philip Cox
14,818 Points

You should very much consider using the PHP PDO object to connect to your database. It is much safer than this older method. Work through the PHP courses here on Treehouse, and all will be explained.

yes i right it ... i made a form and i put data on it then it got connect then every time i fill up the form data does not enter into mysql phpmyadmin

Andrew Shook
Andrew Shook
31,709 Points

Fixed formatting issues and removed the password from you sql connection.

1 Answer