11 Replies - 1458 Views - Last Post: 21 February 2011 - 05:30 PM Rate Topic: -----

#1 dannybarh  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 19-February 11

my insert.php script will not work

Posted 19 February 2011 - 08:56 AM

Hi all,

i have been trying to insert data into my db,but it wont work.

can anyone tell me why,pls.


HTML form

<form action="insert.php?nav=mail" method="post"  enctype=multipart/form-data>
                        <table width="100%" border="0" align="left" cellpadding="1" cellspacing="1" class="bodytxt">
                          <tr>
                            <td height="30" colspan="3" align="left" bgcolor="#999999"><strong><font color="#666666">&nbsp;&nbsp;</font><span class="style2"><font color="#FFFFFF">Compose Mail </font></span> </strong></td>
                          </tr>
                          <tr>
                            <td>&nbsp;</td>
                          </tr>
                          <tr>
                            <td><fieldset>
                              <legend><span class="style1">Recepent's Address </span></legend>
                              <table width="100%" border="0" cellpadding="1" cellspacing="1" class="bodytxt">
                                <tr>
                                  <td width="29%" align="right">To:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
                                  <td width="71%"><label>
                                    <input name="username" type="text" id="username" class="bodytxt" size="60" maxlength="200" />
                                  </label></td>
                                  
                                </tr>
                                <tr>
                                  <td width="29%" align="right">Subject:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
                                  <td><input name="title" type="text" id="title" class="bodytxt" size="60" /></td>
                                  
                                </tr>
                              </table>
                            </fieldset> </td>
                          </tr>
                          <tr>

                            <td height="203"><table width="90%" border="0" align="center" cellpadding="5" cellspacing="1" bordercolor="#666666" class="bodytxt" hspace="0">
                              <tr>
                               
                                <td width="45%" height="196" align="center" valign="top"><fieldset>
                                  <legend><span class="style1">Compose Mail Below</span></legend>
                                  <label>
                                  <textarea name="description" id="description" rows="10" cols="65"></textarea>
                                  </label>
                                </fieldset></td>
                              </tr>
                              <tr>
                                <td height="28" align="center" valign="top">&nbsp;</td>
                              </tr>
                              <tr>
                                <td height="28" align="center" valign="top"><input name="submit" type="submit" class="bodytxt"  value="Send Massage" />
&nbsp;
<input name="reset" type="reset" class="bodytxt" value="Cancel" /></td>
                              </tr>
                            </table>
							</td>
                          </tr>
                          <tr>
                           
                            <td></td>
                          </tr>
						   <tr>
						     
						     <td></td>
						     </tr>
						   <tr>
						     <td>&nbsp;</td>
						     </tr>
                        </table>
                        </form>




Below is my insert.php
<?php
session_start();
$navtype = $_GET['nav'];
include "config.php";
?>

<?php

	if($navtype == "mail")
	{
		 $username = $_POST['username'];
		 $title = $_POST['title'];
		 $description = $_POST['description'];
	 
		 $sql = ("INSERT INTO inbox (username, title, description, datesent, timesent) 
		 
		 VALUES('$username', '$title', '$description', NOW(), CURTIME(),") ;
		 mysql_query($sql);
		 
		 echo "<center><p><br><br><br><strong>The new $title has been added to the database</strong></p></form>";
	}
?>



MOD EDIT: When posting code, use CODE tags, not PHP tags or HTML tags!

:code:

This post has been edited by JackOfAllTrades: 19 February 2011 - 09:04 AM


Is This A Good Question/Topic? 0
  • +

Replies To: my insert.php script will not work

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: my insert.php script will not work

Posted 19 February 2011 - 09:05 AM

Read this and get back to us.
Was This Post Helpful? 1
  • +
  • -

#3 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: my insert.php script will not work

Posted 19 February 2011 - 10:02 AM

Your insert query is malformed. You're building it as INSERT INTO inbox (username, title, description, datesent, timesent)VALUES('$username', '$title', '$description', NOW(), CURTIME(), when you should be building it as INSERT INTO inbox (username, title, description, datesent, timesent)VALUES('$username', '$title', '$description', NOW(), CURTIME()). Of course, you'd have known that if you had tested your query submission on line 18 of your presented insert.php. Most basic way to test this is changing that line to be mysql_query($sql) or die(mysql_error();, but it is generally inadvisable to leave it that way on a production server, as it presents identifying information about your database to any potential malicious users. Something like this is more advisable:

$qsuccess = mysql_query($sql);
if(!$qsuccess)
{
     error_log("Query failed.  MySQL error: " . mysql_error() . "\nQuery submitted: " . $sql . "\n", 3);
     die("There was an error with the database.  It has been logged.");
}

Was This Post Helpful? 2
  • +
  • -

#4 dannybarh  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 19-February 11

Re: my insert.php script will not work

Posted 20 February 2011 - 04:30 PM

thanks Valek,

but when i run the code this is what i get
Parse error: syntax error, unexpected T_STRING in E:\xampp\htdocs\facelink\admin\userindex\insert.php on line 15


i check online,some think it could be a slach or something.
this is the insert.php after your replay to my problem.

am new to php/mysql,so please be ... and thanks for your help.
<?php
session_start();
$navtype = $_GET['nav'];
include "config.php";
?>

<?php

	if($navtype == "mail")
	{
		 $username = $_POST['username'];
		 $title = $_POST['title'];
		 $description = $_POST['description'];
	 
		 $sql = INSERT INTO inbox (username, title, description, datesent, timesent)VALUES('$username', '$title', '$description', NOW(), CURTIME());
		 $qsuccess = mysql_query($sql);
if(!$qsuccess)
{
     error_log("Query failed.  MySQL error: " . mysql_error() . "\nQuery submitted: " . $sql . "\n", 3);
     die("There was an error with the database.  It has been logged.");
	 
		 echo "<center><p><br><br><br><strong>The new $title has been added to the database</strong></p></form>";
	}
?>

Was This Post Helpful? 0
  • +
  • -

#5 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5246
  • View blog
  • Posts: 27,055
  • Joined: 10-May 07

Re: my insert.php script will not work

Posted 20 February 2011 - 04:34 PM

I don't see a closing bracket for your test on the navtype variable.
Was This Post Helpful? 2
  • +
  • -

#6 dannybarh  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 19-February 11

Re: my insert.php script will not work

Posted 20 February 2011 - 05:06 PM

View Postno2pencil, on 20 February 2011 - 04:34 PM, said:

I don't see a closing bracket for your test on the navtype variable.


All the navtype is in the closing tag,
which one are u referring to?
Was This Post Helpful? 0
  • +
  • -

#7 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: my insert.php script will not work

Posted 20 February 2011 - 05:29 PM

Look at your conditionals. Count your brackets. You'll notice you have two opening brackets and one closing bracket, just as no2pencil told you.

You're also missing quotes around your query string from the looks of it.

This post has been edited by Valek: 20 February 2011 - 05:31 PM

Was This Post Helpful? 4
  • +
  • -

#8 dannybarh  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 19-February 11

Re: my insert.php script will not work

Posted 21 February 2011 - 01:09 AM

Thanks for all your time, its been very useful and am very grateful.
Am learning very fast and am very grateful for the commitment Valek and no2pencil and others. I have gain some useful insight when it comes to these errors.

However,this is what i am getting
There was an error with the database. It has been logged.


when i run the code.

am very grateful for your concern.


<?php
session_start();
$navtype = $_GET["nav"];
include "config.php";
?>

<?php

	if($navtype == "mail")
	{
	
		 $username = $_POST['username'];
		 $title = $_POST['title'];
		 $description = $_POST['description'];
	 
		 $sql = "INSERT INTO inbox (username, title, description, datesent, timesent)
		 VALUES('$username', '$title', '$description', NOW(), CURTIME())";
		 
		 "$qsuccess = mysql_query($sql)";

		 
if(!$qsuccess)
{
     error_log("Query failed.  MySQL error: " . mysql_error() . "\nQuery submitted: " . $sql . "\n", 3);
     die("There was an error with the database.  It has been logged.");
}

		 echo "<center><p><br><br><br><strong>The new $title has been added to the database</strong></p></form>";
	
	}
?>

Was This Post Helpful? 0
  • +
  • -

#9 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5246
  • View blog
  • Posts: 27,055
  • Joined: 10-May 07

Re: my insert.php script will not work

Posted 21 February 2011 - 06:33 AM

That's your own self coded error message.

Quote

if(!$qsuccess)
{
     error_log("Query failed.  MySQL error: " . mysql_error() . "\nQuery submitted: " . $sql . "\n", 3);
     die("There was an error with the database.  It has been logged.");
}



I would echo the $sql variable to see what the sql statement contained, & visually verify that it's correct. Most likely one of your input variables is empty, or there is a syntax error. Most likely the problem is that you THINK it should work, & have not validated the statement.
Was This Post Helpful? 1
  • +
  • -

#10 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: my insert.php script will not work

Posted 21 February 2011 - 03:21 PM

That error_log() line writes the actual SQL error to the log where all of your PHP errors are written (provided you have the log_errors directive on in php.ini, which you should, it will write to the error log specified in the error_log directive in php.ini). It will tell you both why the query failed, and what query it attempted to execute.

It's safe to echo the $sql variable's contents or mysql_error() when you're developing a site, but when other users are actually using it, it is unsafe because it reveals information about how your site/application is built. That is why I provided you the code I did for debugging the error, because that code is safe for a production website.
Was This Post Helpful? 2
  • +
  • -

#11 dannybarh  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 19-February 11

Re: my insert.php script will not work

Posted 21 February 2011 - 05:15 PM

am very grateful for your concern.Thanks.
Was This Post Helpful? 0
  • +
  • -

#12 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2925
  • View blog
  • Posts: 10,109
  • Joined: 08-August 08

Re: my insert.php script will not work

Posted 21 February 2011 - 05:30 PM

Ok, I'll post the obligatory warning about SQL injection attacks:
http://en.wikipedia....i/SQL_injection
http://www.dreaminco...duction-to-pdo/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1