Jump to content

whats wrong with my query?


dadamssg

Recommended Posts

so im trying to learn i can't..for the life of me..figure out why i cant execute this..its a simple two field form.

 

i get an unexpected T_VARIABLE error on the defining the $sql line

 

<?php

/* File: processtest.php*/

include("caneck.inc");

$title = $_POST['title'];

$description = $_POST['description'];

$cnx = mysqli_connect($host,$user,$passwd,$dbname)
         

$sql = "INSERT INTO test (title, description) VALUES ('$title','$description')";

$result = mysqli_query($cnx,$sql)
         or die ("Couldn't execute query.");

include("tesdisplay.inc");

Print "The title, $title, and the description, $description, 
       has been entered.";
?>

Link to comment
https://forums.phpfreaks.com/topic/141293-whats-wrong-with-my-query/
Share on other sites

sorry that was the wrong code...i that but you were right, it was missing the semicolon

 

but with this i get "Couldn't execute query"

 

<?php

/* File: processtest.php*/

include("caneck.inc");

$title = $_POST['title'];

$description = $_POST['description'];

$cnx = mysqli_connect($host,$user,$passwd,$dbname)
         

$sql = "INSERT INTO test (title, description) VALUES ('$title','$description')";

$result = mysqli_query($cnx,$sql)
         or die ("Couldn't execute query.");

include("tesdisplay.inc");

Print "The title, $title, and the description, $description, 
       has been entered.";
?>

 

 

You need to check your form is actually being submitted. A better examples of your simple query would be....

 

<?php

/* File: processtest.php*/

if (isset($_POST['submit'])) { // <-- name of submit button
  include "caneck.inc";

  $title = mysql_real_escape_string($_POST['title']);
  $description = mysql_real_escape_string($_POST['description']);

  $cnx = mysqli_connect($host, $user, $passwd, $dbname) or die(mysqli_error($cnx);

  $sql = "INSERT INTO test (title, description) VALUES ('$title','$description')";

  if ($result = mysqli_query($cnx,$sql)) {
    include "tesdisplay.inc";
    print "The title, $title, and the description, $description, has been entered.";
  } else {
    echo "Query failed<br />$sql<br />" . mysqli_error($cnx);
  }

}

?>

 

ps: I'm not sure where your learning php from but include files should not be named with the inc extension, use php.

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.