1

I am trying to submit this form data to another database using php on xampp server. Once i click on submit it shows waiting for localhost and does not even save the entries in the database. I have made the changes in the xampp to listen on port 8080.I am pretty new to php. Thanks in advance!

Here is my html

<!DOCTYPE html>
<html>
<body>

<form action="submit.php" method=post">
 First name:<br>
<input type="text" name="firstname" >
<br>
 Last name:<br>
<input type="text" name="lastname" >
<br><br>
<input type="submit" value="submit">
</form>
</body>
</html> 

Here is my php code

<?php
$x=$_POST['firstname'];
$y=$_POST['lastname'];
$servername= 'localhost';
$username = 'root';
$password = '';
$dbname = 'db1';
$port = 8080;

//$mysqli = new mysqli("localhost", "username", "password", "dbname");

$conn = new mysqli($servername, $username, $password, $dbname, $port);

echo "Connection successful!" . "<bc>";
$sql = "INSERT INTO user (fname, lname) VALUES ('$x', '$y')";

if($conn->query($sql) === TRUE){
 echo "New record created successfully";
} else {
 echo "<bc> Error: " .  $sql . "<br>" . $conn->error;
}

$conn->close();
?> 

I also seem to get the following errors: Notice: Undefined index: first in F:\Xampp\htdocs\Project\submit.php on line 3

Notice: Undefined index: last in F:\Xampp\htdocs\Project\submit.php on line 4

Warning: mysqli::__construct(): MySQL server has gone away in F:\Xampp\htdocs\Project\submit.php on line 13

Warning: mysqli::__construct(): Error while reading greeting packet. PID=4572 in F:\Xampp\htdocs\Project\submit.php on line 13

Warning: mysqli::__construct(): (HY000/2006): MySQL server has gone away in F:\Xampp\htdocs\Project\submit.php on line 13

Fatal error: Maximum execution time of 120 seconds exceeded in F:\Xampp\htdocs\Project\submit.php on line 13

  • did you start your xampp? – Sajad Haibat Oct 13 '19 at 04:14
  • Yes i have started it. @sajadHaibat – Prathamesh Koyande Oct 13 '19 at 04:48
  • To debug, you need to divide your problem in parts. Let's forgot the .html form and test your .php script. Try changing $_POST for $_REQUEST and open the URL _http://localhost/submit.php?firstname=test&lastname=test_ to see if your script is running. – Leopoldo Sanczyk Oct 13 '19 at 04:49
  • @LeopoldoSanczyk If i run your URL it's showing object not found. However I have kept bot the files that is the index and submit file in the xampp root directory which is the htdocs. – Prathamesh Koyande Oct 13 '19 at 04:52
  • You edited the question. Now I can see your script is inside the "Project" folder. So, the test URL should be http://localhost/Project/submit.php?firstname=test&lastname=test – Leopoldo Sanczyk Oct 13 '19 at 04:57
  • 1
    Notice: Undefined index: firstname in F:\Xampp\htdocs\Project\submit.php on line 2 Notice: Undefined index: lastname in F:\Xampp\htdocs\Project\submit.php on line 3 I am getting these errors on the page. Please try to resolve this. The rows are getting created in the database but the values passed from the form are not getting saved in the table. – Prathamesh Koyande Oct 13 '19 at 05:04
  • Seeing the last errors you added, seems you changed your variables names. `firstname` is now just `first`, and `lastname` is `last`. So, change the URL arguments to fist=test&last=test, or change the line 3 and 4 of the code to use `$_REQUEST['firstname']` and `$_REQUEST['lastname']` again. Variable names must match. – Leopoldo Sanczyk Oct 13 '19 at 05:07
  • That's good. Means your XAMPP is working, and your MySQL part of the code too. You just need to get the variable values. $_REQUEST can work with submited arguments from a form with method 'post', or with arguments passed in the URL. If you use $_POST instead you need to be sure your .html part is working too. – Leopoldo Sanczyk Oct 13 '19 at 05:11
  • You can use the PHP function `var_dump($someVar)` as an easy way to check if variables are set. Else you should do somethink like `echo "Is someVar set? ".isset($someVar);` – Leopoldo Sanczyk Oct 13 '19 at 05:18
  • 1
    Thanks a Lot @LeopoldoSanczyk. Your suggestion about the name attribute seems to be working for me now. I had not added the name attribute in the html code which i added now and matched it with the variables in the php file. – Prathamesh Koyande Oct 13 '19 at 06:01

1 Answers1

-1

Make sure to use the right MySQL port (the default is 3306) and that the user table exists (below sample ensures that as well), at last change in your HTML the part method=post" to method="post" (a double quote is missing).

Try below:

<?php
$x=$_POST['firstname'];
$y=$_POST['lastname'];
$servername= 'localhost';
$username = 'root';
$password = '';
$dbname = 'db1';
$port = 3306;

$conn = new mysqli($servername, $username, $password, $dbname, $port);
echo "Connection successful!" . "<bc>";

// Ensures table exists:
$conn->query('CREATE TABLE IF NOT EXISTS `user` (fname VARCHAR(40) NOT NULL, lname VARCHAR(20) NOT NULL);');

$sql = "INSERT INTO user (fname, lname) VALUES ('$x', '$y')";
if($conn->query($sql) === TRUE){
 echo "New record created successfully";
} else {
 echo "<bc> Error: " .  $sql . "<br>" . $conn->error;
}

$conn->close();
?>

Anyway, I suggest you use PHP's PDO and prepared queries (to prevent SQL injection attacks), which would turn your code into:

<?php
$x=$_POST['firstname'];
$y=$_POST['lastname'];
$servername= 'localhost';
$username = 'root';
$password = '';
$dbname = 'db1';
//$port = 3306;

try {
 $pdo = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
 echo "Connection successful!" . "<bc>";

 // Ensures table exists:
 $pdo->query('CREATE TABLE IF NOT EXISTS `user` (fname VARCHAR(40) NOT NULL, lname VARCHAR(20) NOT NULL);');

 // prepare the statement and/or command
 $sql = 'INSERT INTO user (fname, lname) VALUES (:x, :y)';
 $statement = $pdo->prepare($sql);
 // pass value to the command
 $statement->bindParam(':x', $x, PDO::PARAM_STR);
 $statement->bindParam(':y', $y, PDO::PARAM_STR);
 // execute the prepared statement
 if($statement->execute() === TRUE){
  echo "New record created successfully";
 } else {
  echo "<bc> Error: " .  $sql . "<br>" . join(' ', $statement->errorInfo());
 }

 $statement->closeCursor();
} catch (PDOException $e) {
 die("Error occurred:" . $e->getMessage());
}

?>

Top-Master
  • 7,611
  • 5
  • 39
  • 71
  • I am getting the message of connection successful. But the data inserted in the form is not getting saved in the database table. I am getting these errors on the submit.php Notice: Undefined index: firstname in F:\Xampp\htdocs\Project\submit.php on line 2 Notice: Undefined index: lastname in F:\Xampp\htdocs\Project\submit.php on line 3 Connection successful!New record created successfully – Prathamesh Koyande Oct 13 '19 at 05:01
  • @PrathameshKoyande, have updated answer! – Top-Master Oct 13 '19 at 05:19
  • 1
    Thanks a lot Borther. Your first code seems to be working for me now. I was not having the name attribute in html form. Anyways my bad and also thanks for helping me out. – Prathamesh Koyande Oct 13 '19 at 05:59
  • and if you want to resolve those warning, you can use isset so that the codes only goes toward them once you submit the form. – Naser Nikzad Oct 13 '19 at 07:27