?
Solved

query not executing in mysql

Posted on 2003-03-20
4
Medium Priority
?
193 Views
Last Modified: 2009-12-16
Hi below are is a php file that inserts information into a table in a mysql database.
It seems to be working (no error messages are generated) but when i do a select statement in the mysql database the query in the php file does'nt seem to be executed.
Any help is much appreciated.
Cheers


----------------------php file (submitform.php)-----------------------

<html>
<body>
<?php

if ((isset($_POST["traintime"]))
   &&(isset($_POST["traintype"]))
   &&(trim($_POST["traintime"])!='')
   &&(trim($_POST["traintype"])!='')):

       $traintime=addslashes($_POST["traintime"]);
       $traintype=addslashes($_POST["traintype"]);
       $db = mysql_connect ("localhost");
       mysql_select_db ("mysql" ,$db);
       $sql = "INSERT INTO timetable2 (traintime, traintype) VALUES ($traintime, $traintype)";
       $result = mysql_query($sql);
       printf("<tr><td>%s\n", $traintime);
       print (" ");
       printf("<tr><td>%s\n", $traintype);
else:
   print ("Error in post data");
endif;
?>
</body>
</html>


---------------------------html file ------------------
<html>
<head>
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!--

function sendform()
{
 if ((document.form.traintime.value!='')&&(document.form.traintype.value!=''))
 {
    document.form.submit();
 }
 else
 {
   alert("You must enter a first and last name");
 }
}


-->
</SCRIPT>
</head>
<body>
<form name="form" action="submitform.php" method="post">
First Name: <input type="text" name="traintime" size="25"
maxlength="25">
Last Name: <input type="text" name="traintype" size="25"
maxlength="25">
<p>
<input type="button" onClick="javascript:sendform()">
</form>
</body>
</html>
0
Comment
Question by:irish_paddy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 3

Expert Comment

by:WysG
ID: 8177178
Hi

I'll start with a personnal advice : Don't mess around in the mysql database. I suggest that you create another database where to stock you data, and don't mess with the database that mysql is using to maintain is system. ^_^

As for your problem, if you get no error message i don't know what it could be, try to echo your $sql

Another thing

This
printf("<tr><td>%s\n", $traintime);
print (" ");
printf("<tr><td>%s\n", $traintype);


should be

printf("<table><tr><td>%s</td></tr>\n", $traintime);
printf("<tr><td>%s</td></tr></table>\n", $traintype);
0
 
LVL 15

Accepted Solution

by:
VGR earned 120 total points
ID: 8177305
agree 3000% with the above. Create yourself a database 8-) At least, you know that the 'mysql' one is connectable all the time :D

Let me review your code :

<?php

if ((isset($_POST["traintime"]))
  &&(isset($_POST["traintype"]))
  &&(trim($_POST["traintime"])!='')
  &&(trim($_POST["traintype"])!='')): // I suggest you use { and } and not :
//here note that PHP does NOT do what Turbo-Pascal and Delphi do : partial Boolean evaluation (optimization). This means that the above will issue a NOTICE or WARNING (probably) about $_POST[] being not set and trimed anyway

      $traintime=addslashes($_POST["traintime"]);
      $traintype=addslashes($_POST["traintype"]);
      // so I guess that your magic_quoting is off or else you'll addslashes to already-addslashed values 8-)
      $db = mysql_connect ("localhost"); // please do add "or die("failed to connect ".mysql_error());"
      mysql_select_db ("mysql" ,$db); // please do add "or die("failed to select DB ".mysql_error());" // would be surprising given it's "mysql" DB
      $sql = "INSERT INTO timetable2 (traintime, traintype) VALUES ($traintime, $traintype)";
      $result = mysql_query($sql); // please do add "or die("bad query '$query' : ".mysql_error());"
      printf("<tr><td>%s\n", $traintime); // badly terminated table line construct, </td></tr> please
      print (" "); // useless in a TABLE (would print touside of it) and meaningless in HTML : white space is &nbsp; and CRLF is <BR>
      printf("<tr><td>%s\n", $traintype);
else:// I suggest you use { and } and not :
  print ("Error in post data");
endif;// I suggest you use { and } and not :
?>
0
 
LVL 15

Expert Comment

by:VGR
ID: 8177317
ALSO the most classical error of all : you forgot the QUOTES around values assed to the SQL. It's EVIL :D

Your request should be :

$sql = "INSERT INTO timetable2 (traintime, traintype) VALUES ('$traintime', '$traintype')";
     
0
 
LVL 15

Expert Comment

by:VGR
ID: 8177329
the most classical and trivial errors are always the ones you see last 8-))
0

Featured Post

WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question