|
-
Mar 15th, 2005, 07:37 AM
#1
Thread Starter
New Member
SQL Hidden Problem
Right. This script just adds some information into a database. The information comes from text-boxes. My problem is that, when everything works until I hit the Submit button. The entries in the text-boxes disappear and nothing happens. I wrote a viewing script to check and see if the values were actually entered into the database, but no such luck. Any idea what I'm doing wrong?
PHP Code:
<?php
if ($_POST['submit'])
{
//connect to mysql
$connect = mysql_connect('localhost','username','password');
//verify connection
if (!$connect)
{
echo 'Unable to connect to MySQL<br>' . mysql_error() . '';
}
//select a database
$select = mysql_select_db('database');
//verify db selection
if (!$select)
{
echo 'Unable to select a database<br>' . mysql_error() . '';
}
//get values
//to be inserted into database
$title = $_POST["title"];
$author = $_POST["author"];
$message = $_POST["message"];
$tdate = $_POST['tdate'];
//add values into database
//add them in the corresponding fields
$query = "INSERT INTO news (id,title,author,message,tdate)" . "VALUES ('NULL','$title','$author','$message','$tdate')";
$r_query = mysql_query($query);
//veryify query
if ($r_query)
{
echo 'Query has been completed.';
}
}
else
{
?>
<form method="post" action="sql_add.php">
<table>
<tr>
<td>Title:</td>
<td><input type="text" name="title"></td>
</tr>
<tr>
<td>Author:</td>
<td><input type="text" name="author"></td>
</tr>
<tr>
<td>Date:</td>
<td><input type="text" name="tdate"></td>
</tr>
<tr>
<td>Message:</td>
<td><input type="text" name="message"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="submit"></td>
</tr>
</table>
</form>
<?php
}
?>
Thanks ahead of time,
--Brendan
-
Mar 15th, 2005, 08:00 AM
#2
Junior Member
Re: SQL Hidden Problem
Is the id-field in the database a varchar type or an integer type? Why are you including it there if you're just going to plonk 'NULL' into it?
If there is a way to solve your problems, there is no need to worry; if there is no way to solve your problems, there is no point to worry.
-
Mar 15th, 2005, 08:15 AM
#3
Re: SQL Hidden Problem
KTotte is right. You can omit the ID from your field list if the database is gernetating it automatically.
The reason your form isn't working is because you are not checking the submit variable properly. If the user submits the form then the variable $_POST['name'] is created and set to a null string ''. If the form is not submitted then the $_POST['name'] variable is not created at all. In both cases testing for the variable with an IF statement will return false, as a null string is evaluated as a false.
Instead use the isset() function to test whether or not the variable was actually created:
PHP Code:
if (isset($_POST['submit']))
-
Mar 15th, 2005, 02:34 PM
#4
Thread Starter
New Member
Re: SQL Hidden Problem
Thanks for the help you guys.
As for the SQL thingy, I'm just starting to learn SQL (or relearn it rather) and I based that code of off a tutorial I read. The tutorial used NULL for the ID field - but if it's not needed, that's still fine.
Thanks again!
--Brendan
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|