Jump to content

[SOLVED] Basic form (INSERT INTO). One works, another doesn't.


suttercain

Recommended Posts

Hi everyone,

 

I have a form that INSERTs information into my database. This one works....

<?php

extract($_REQUEST);

if ( isset($subject) && isset($body) ) {
mysql_connect("localhost","root");
mysql_select_db( "messages" );

  mysql_query( "INSERT INTO messages (subject, body) ".
   "VALUES ('$subject', '$body' )" );
   $status = "Message <i>$subject</i> has been posted<br><br>";
}

?>
<html><head><title>message</title></head>
<body>

<h1>Post a Message</h1>

<strong><?php echo $status ?></strong>

<form action="<?php echo $_SERVER['SELF'] ?>">

Subject	<input type="text" 
          name="subject" 
          size="35">
           <br><br>
 <textarea name="body" cols="40" rows="15">
          </textarea>

<br><br>
<input type="submit" value="Post">
</form>

</body>
</html>

 

But when I change a little bit of it and use another database... this one doesn't work...

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<?php
extract($_REQUEST);

if (isset($title) && isset($text) && isset($plot) && isset($cover_artists)) {
mysql_connect("localhost","root", NULL);
mysql_select_db( "comics" );

mysql_query ("INSERT INTO comics (title, text, plot, cover_artists) " . 
		 "VALUES ('$title', '$date', '$text', '$plot', '$cover_artists')");

$status = "Message $title has been uploaded";
}
?>
<strong><?php echo $status ?></strong>



<form action="<?php echo $_SERVER['SELF'] ?>">
Title: <input type="text" name="title" size="35"><br><br>
Text: <input type="text" name="text" size="35"><br><br>
Plot: <input type="text" name="plot" size="35"><br><br>
Cover Artists: <input type="cover_artists" name="title" size="35"><br><br>
<input type="submit" value="Post">
</form>
</body>
</html>

 

Why is the first one working... yet, the second one isn't?

 

Thanks

Try this to return an error of some sort for us to work from:

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<?php
extract($_REQUEST);

if (isset($title) && isset($text) && isset($plot) && isset($cover_artists)) {
mysql_connect("localhost","root", NULL) or die('Error Connecting: '.mysql_error());
mysql_select_db( "comics" ) or die('Error Selecting DB: '.mysql_error());;

mysql_query ("INSERT INTO comics (title, text, plot, cover_artists) " . 
		 "VALUES ('$title', '$date', '$text', '$plot', '$cover_artists')") or die('Error In Query: '.mysql_error());;

$status = "Message $title has been uploaded";
}
?>
<strong><?php echo $status ?></strong>



<form action="<?php echo $_SERVER['SELF'] ?>">
Title: <input type="text" name="title" size="35"><br><br>
Text: <input type="text" name="text" size="35"><br><br>
Plot: <input type="text" name="plot" size="35"><br><br>
Cover Artists: <input type="cover_artists" name="title" size="35"><br><br>
<input type="submit" value="Post">
</form>
</body>
</html>

 

 

This is always a good way to retrieve the error without having to look over you code 100 times and a way to self help yourself as most errors are self explanatory.

 

 

 

 

Regards

Liam

Try this to see if you if statement is correct..

 

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<?php
extract($_REQUEST);
if (isset($title) && isset($text) && isset($plot) && isset($cover_artists)) {
echo ("Correct!!!");
} else {
echo ("Failed!!!");
}
?>
</body>
</html>

 

 

 

Liam

I ran this:

 

<?php
extract($_REQUEST);

if (isset($title) && isset($text) && isset($plot) && isset($cover_artists)) {
echo ("Correct!!!");
} else {
echo ("Failed!!!");
}

if (isset($title) && isset($text) && isset($plot) && isset($cover_artists)) {
mysql_connect("localhost","root", NULL) or die('Error Connecting: '.mysql_error());
mysql_select_db( "comics" ) or die('Error Selecting DB: '.mysql_error());;

mysql_query ("INSERT INTO comics (title, text, plot, cover_artists) " . 
		 "VALUES ('$title', '$date', '$text', '$plot', '$cover_artists')") or die('Error In Query: '.mysql_error());;

$status = "Message $title has been uploaded";
}
?>
<strong><?php echo $status ?></strong>



<form action="<?php echo $_SERVER['SELF'] ?>">
Title: <input type="text" name="title" size="35"><br><br>
Text: <input type="text" name="text" size="35"><br><br>
Plot: <input type="text" name="plot" size="35"><br><br>
Cover Artists: <input type="cover_artists" name="title" size="35"><br><br>
<input type="submit" value="Post">
</form>

 

And it echoed failed! and reloaded the form

 

hmm....

I got it working, my bad.

 

Thanks shocker for the help.

 

Quick question though... does anyone know how to do a "Preview" page before actually submitting it the user enters the info, hits submit, verifies the information is correct, hit's submit again to enter into MySQL?

 

Here is the working code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<?php
extract($_REQUEST);

if (isset($title) && isset($text) && isset($plot) && isset($cover_artists)) {
echo ("Correct!!!");
} else {
echo ("Failed!!!");
}

if (isset($title) && isset($text) && isset($plot) && isset($cover_artists)) {
mysql_connect("localhost","root", NULL) or die('Error Connecting: '.mysql_error());
mysql_select_db( "comics" ) or die('Error Selecting DB: '.mysql_error());;

mysql_query ("INSERT INTO comics (title, text, plot, cover_artists) " . 
		 "VALUES ('$title', '$text', '$plot', '$cover_artists')") or die('Error In Query: '.mysql_error());;

$status = "Message $title has been uploaded";
}
?>
<strong><?php echo $status ?></strong>



<form action="<?php echo $_SERVER['SELF'] ?>">
Title: <input type="text" name="title" size="35"><br><br>
Text: <input type="text" name="text" size="35"><br><br>
Plot: <input type="text" name="plot" size="35"><br><br>
Cover Artists: <input type="text" name="cover_artists" size="35"><br><br>
<input type="submit" value="Post">
</form>
</body>
</html>

no Problem

 

 

You could store it in a tmp table then transfer or you could hold all the info in session variables and on submit you actually insert the information..

 

Better using session variables though.

 

 

Liam

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.