My insert.php 4 inserting into database doesnt work here is the code below tell me

Phumlani M

New member
what is wrong wit it? Register.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Registration</title>

</head>
<body>
<h1>Register</h1>
<form action="insert.php" method="post">
<p>First Name:<br>
<input type = "text" name = "firstN"/>
</p>
<p>Last:<br>
<input type = "text" name = "lastN"/>
</p>
<p>Business Name:<br>
<input type = "text" name = "busName"/>
</p>
<p>Email:<br>
<input type = "text" name = "e_mail"/>
</p>
<p>Contact Number:<br>
<input type = "text" name = "contactNo"/>
</p>
<p>Username:<br>
<input type = "text" name = "uname"/>
</p>
<p>Password:<br>
<input type = "password" name = "passw"/>
</p>
<p>
<input type = "button" name = "add" value = "Submit" />
</p>
</form>
</body>
</html>

insert.php

<?php

$lh = "localhost";
$uname = "root";
$passw = "";
$db = "fivestep";

$firstN = $_POST['firstN'];
$lastN = $_POST['lastN'];
$busName = $_POST['busName'];
$e_mail = $_POST['e_mail'];
$contactNo = $_POST['contactNo'];
$uname = $_POST['uname'];
$passw = $_POST['passw'];

$con = mysql_connect($lh, $uname, $passw);

if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db($db, $con);

$sql = "INSERT INTO t_user(u_id, firstName, lastName, busName, eAddress, contactNo, uname, passw)
VALUES ('', $firstN, $lastN, $busName, $e_mail, $contactNo, $uname, $passw)";

mysql_query($sql, $con) or die('Error: ' . mysql_error());

mysql_close($con);

?>
 
Looking at a glance, it feels like in this statement

$sql = "INSERT INTO t_user(u_id, firstName, lastName, busName, eAddress, contactNo, uname, passw)
VALUES ('', $firstN, $lastN, $busName, $e_mail, $contactNo, $uname, $passw)";

u should have given ' at start and end of those database fields where it is inserting chars/strings. Like ,

$sql = "INSERT INTO t_user(u_id, firstName, lastName, busName, eAddress, contactNo, uname, passw)
VALUES ('', '$firstN', '$lastN', '$busName', '$e_mail', '$contactNo', '$uname', '$passw')";

im not sure whether all these fields are varchar or not. give single quotes on where it is.

a better way to debug is to run the query directly in mysql if u have reasons to believe that it's the query that is the culprit.

Hope this helps...........
 
here lies the problem:
VALUES ('', $firstN, $lastN, $busName, $e_mail, $contactNo, $uname, $passw)";
it should be:
VALUES ('',".$firstN.", ".$lastN.",". $busName.", ".$e_mail.",". $contactNo.", ".$uname.", ".$passw.")";
HAPPY CODING.
 
Looking at a glance, it feels like in this statement

$sql = "INSERT INTO t_user(u_id, firstName, lastName, busName, eAddress, contactNo, uname, passw)
VALUES ('', $firstN, $lastN, $busName, $e_mail, $contactNo, $uname, $passw)";

u should have given ' at start and end of those database fields where it is inserting chars/strings. Like ,

$sql = "INSERT INTO t_user(u_id, firstName, lastName, busName, eAddress, contactNo, uname, passw)
VALUES ('', '$firstN', '$lastN', '$busName', '$e_mail', '$contactNo', '$uname', '$passw')";

im not sure whether all these fields are varchar or not. give single quotes on where it is.

a better way to debug is to run the query directly in mysql if u have reasons to believe that it's the query that is the culprit.

Hope this helps...........
 
Back
Top