...

View Full Version : form and php



cgibie
02-10-2006, 04:10 AM
I tried to make a form that allows user to add their first and last name to my db... it keeps telling me that the query failed. Help please!



<html>
<head>
<title>Add New MySQL User</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
if(isset($_POST['add']))
{
include 'config.php';
include 'opendb.php';

$fname = $_POST['fname'];
$lname = $_POST['lname'];

$query = "INSERT INTO Member (mfname, mlname, insert_priv, insert_ priv) VALUES ('$fname', '$lname', 'Y', 'Y')";
mysql_query($query) or die('Error, insert query failed');

$query = "FLUSH PRIVILEGES";
mysql_query($query) or die('Error, insert query failed');

include 'closedb.php';
echo "New MySQL user added";
}
else
{
?>
<form method="post">
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr>
<td width="100">First Name</td>
<td><input name="fname" type="text" id="fname"></td>
</tr>
<tr>
<td width="100">Last Name</td>
<td><input name="lname" type="text" id="lname"></td>
</tr>
<tr>
<td width="100"> </td>
<td> </td>
</tr>
<tr>
<td width="100"> </td>
<td><input name="add" type="submit" id="add" value="Add Me"></td>
</tr>
</table>
</form>
<?php
}
?>
</body>
</html>

Brandoe85
02-10-2006, 04:35 AM
Add mysql_error() onto the statement, as well:
mysql_query($query) or die('Error, insert query failed ' . mysql_error());

Good luck;

degsy
02-10-2006, 03:30 PM
mfname, mlname, insert_priv, insert_ priv


Why do you have insert_ priv twice?

cgibie
02-10-2006, 03:53 PM
I have mfname, mlname, and memail field in my Member table. I'm pretty newbie to php and mysql... so I thought maybe 2 insert privs mean that I let user insert to those 2 particular fields mfname and mlname..... but it never got inserted. I don't know what's wrong with that.

degsy
02-10-2006, 03:56 PM
You only need to specify the fields and values that you want to change



$query = "INSERT INTO Member (mfname, mlname) VALUES ('$fname', '$lname')";


or



$query = "INSERT INTO Member (mfname, mlname, memail) VALUES ('$fname', '$lname', '$email')";



http://www.w3schools.com/sql/sql_insert.asp

cgibie
02-20-2006, 10:54 AM
I got error that insert query failed, what's wrong? I did like what you told me. but the row indeed is added to .

cgibie
02-20-2006, 10:56 AM
problem is solved but why there are "????" under my form? and the email address is not saved to the Mysql

cgibie
02-20-2006, 11:46 AM
problem is solved



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum