1

I'm new to PHP and I'm not sure what I'm doing wrong. I can open the html form and give the data but when I hit submit it shows me the php code and the database is (obviously) not updated. I have tried inserting values to the table manually through phpMyAdmin and it works. I have looked online but me syntax looks fine (to me). Am I doing some different wrong? If the mistake is not too obvious is there an efficient way to debug (ie see the errors)?

I have this HTML code

<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Hotel Start Page</title>
</head>

<body>
<form action="customerCreate.php" method="post">
<fieldset><legend>Enter your information in the form below:</legend>
<header align="center">   <b>Customer Sign Up</b></header>
<p><b>Surname:            </b><input type="text" name="surname" size="30" maxlength="40"/></p>
<p><b>Name:               </b><input type="text" name="name" size="30" maxlength="40"/></p>
<p><b>E-mail:             </b><input type="text" name="email" size="30" maxlength="40"/></p>
<p><b>Telephone:          </b><input type="text" name="tel" size="30" maxlength="10"/></p>
<p><b>Password:           </b><input type="password" name="passwd" size="30" maxlength="10"/></p> 
</fieldset>
<div align="center"><input type="submit" name="submit" value="Create Account"/></div>
</form>

</body>

This PHP code

<?php

 $host = 'localhost';
 $username = 'root';
 $password = '';
 $db = 'my_hotel';

 $conn = new mysqli($host,$username,$password,$db);

if ($conn->connect_error) {
  die("Connection Error: " .$conn->connect_error);
}

$sname = $_POST['surname'];
$name = $_POST['name'];
$email = $_POST['email'];
$tel = $_POST['tel'];
$passwd = $_POST['passwd'];

$sql = "INSERT INTO Customer (sname, name, email, tel, passwd) VALUES 
(?,?,?,?,?)";

$stmt = $conn->prepare($sql);
$stmt->bind_param("sssss",$sname,$name,$email,$tel,$passwd);
$stmt->execute();

$cid = "SELECT cid FROM Customers WHERE sname='$sname' AND passwd='$passwd' ";
$result = $conn->query($cid);
$row = $result->fetch_assoc();

echo "Customer Added.<br>";
echo "Your Customer ID is ".$row['cid'];

$stmt->close();
$conn->close();

?>

And this table in a database called My_hotel in phpMyAdmin

CREATE TABLE Customer (
   cid INT AUTO_INCREMENT,
   sname VARCHAR(15),
   name VARCHAR(15),
   email VARCHAR(15),
   tel VARCHAR(15),
   passwd VARCHAR(15),
   PRIMARY KEY (cid)
);
ADyson
  • 57,178
  • 14
  • 51
  • 63
phan801
  • 227
  • 1
  • 9
  • 1
    So, did any error appeared? – Carl Binalla Feb 01 '18 at 09:58
  • 4
    1. You need to add error handling to your database calls and 2. You don't need to make another - sql injectable - database query to get the ID of the last inserted row. And you should never store plain-text passwords, salt and hash them instead. – jeroen Feb 01 '18 at 10:00
  • 2
    Do you have PHP installed and configured into your WebServer? – RiggsFolly Feb 01 '18 at 10:01
  • 1
    @RiggsFolly Yes, PHP is installed and configured – phan801 Feb 01 '18 at 10:13
  • @Swellar Can I view errors somewhere? – phan801 Feb 01 '18 at 10:14
  • 1
    Then does your PHP script's filename end with a `.php` extension? If you used `.html` or anything else PHP will not get called – RiggsFolly Feb 01 '18 at 10:15
  • 1
    @RiggsFolly Yes, it does. – phan801 Feb 01 '18 at 10:15
  • 1
    To see mysql errors, [read this question](https://stackoverflow.com/questions/12227626/how-to-display-mysql-error-in-php) – Michel Feb 01 '18 at 10:26
  • Are you using an IDE (for instance, Eclipse, but any IDE - just use one)? Set a breakpoint and step through the code. Examine your queries, return values, etc, etc. Also wrap your code in `try ... except` and examine the message text of any exception which you catch. When you learn to debug, you won't need to ask us questions like this. – Mawg says reinstate Monica Feb 01 '18 at 10:35
  • 1
    If you're using Chrome or firefox you could take a look at the network tab from the developer tools. On the tab you're able to see all the network calls. So then you should test the function and look at the response. And if the function is called, etc. There you would also be able to see all the errors. To enable errors for `php`, : ` error_reporting(E_ALL); ini_set("display_errors", 1);`. From the network tab -> click the correspondig function called on send. And take a look at the response. – Deathstorm Feb 01 '18 at 10:36
  • @Deathstorm "response" just shows me my php code again.. – phan801 Feb 01 '18 at 10:46
  • @phan801 did you enable the error reporting. Otherwise build a `try and catch` switch around it? – Deathstorm Feb 01 '18 at 10:47
  • ""response" just shows me my php code again" and you didn't think to tell us that???? All quesions to this site shoudl have 3 things - 1) code 2) what it should do 3) what is actually happening. When I read your question, I see that you did not 3) - so are we just supposed to guess what went wrong? – Mawg says reinstate Monica Feb 01 '18 at 10:50
  • 1
    @Mawg I state in my question " I can open the html form and give the data but when I hit submit -->it shows me the php code<-- and the database is (obviously) not updated." – phan801 Feb 01 '18 at 10:55
  • D'oh! So you did. My bad. Sorry – Mawg says reinstate Monica Feb 01 '18 at 10:56

1 Answers1

1

If you get the php code, it must be that your application is not in your web server, so the browser downloads the source file. You should copy/synchronize your files into/with (probably) /var/www/html.

Look into this: http://thisinterestsme.com/php-displayed-in-browser/

C. Miranda
  • 75
  • 1
  • 6
  • From your link "If your PHP code is being displayed in the browser, it means that your server has not been setup to serve PHP scripts", not "your application is not in your web server". but, good link, so +1 – Mawg says reinstate Monica Feb 01 '18 at 10:48
  • The "application not in your web server" part is explained in the last section though :p – C. Miranda Feb 01 '18 at 10:51
  • In response to @RiggsFolly comment question, the OP replied "Yes, PHP is installed and configured" – Mawg says reinstate Monica Feb 01 '18 at 10:51
  • PHP can be installed and configured (typically checked with phpinfo() output in some page) but the application not being in the server has nothing to do with that... Hence my response – C. Miranda Feb 01 '18 at 10:54
  • I am afraid that I don't understand "the application not being in the server" - if "the application" is his PHP file, then it is on the server, because he sees it when he tries to execute it. – Mawg says reinstate Monica Feb 01 '18 at 10:55
  • @CarlosMiranda Thank you, this is definitely the problem! I can get it to pass the data to the database by changing
    to
    . Is there a better way to do it?
    – phan801 Feb 01 '18 at 11:05