Jump to content

HTML PHP MySql error ?


rhwebeditor

Recommended Posts

New to PHP and MySql

My update script gives unhelpful error msg:

 

// Your code here You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

 

What is the problem?  thanks for any help. My Code:

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

 

<html>

 

<head>

<title>Sans Titre</title>

<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />

<meta name="generator" content="HAPedit 3.1">

</head>

<body bgcolor="#FFFFFF">

 

  // Your code here

<?php

// set your infomation.

  $host    =  'localhost';

  $user    =  'root';

  $pass    =  '';

  $database  =  'roscripts';

 

  // connect to the mysql database server.

  $connect = @mysql_connect ($host, $user, $pass);

  $sql = "SELECT * FROM `articles` WHERE `ID` = " . mysql_real_escape_string ( $_GET['ID'] );

 

  mysql_select_db ( $database, $connect );

  if ( @mysql_query ( $sql ) )

  {

    $query = mysql_query ( $sql );

    $row = mysql_fetch_assoc ( $query );

 

    if ( array_key_exists ( '_submit_check', $_POST ) )

    {

      if ( $_POST [ 'article_title' ] != '' && $_POST [ 'article_content' ] != '' )

      {

        mysql_select_db ( $database, $connect );

        $query =  "UPDATE

              `articles`

            SET

              `article_title` = " . mysql_real_escape_string ( $_POST [ 'article_title' ] ) . "

              `article_content` = " . mysql_real_escape_string ( $_POST [ 'article_content' ] ) . "

            WHERE

              `ID` = " . mysql_real_escape_string ( $_GET['ID'] );

 

        if ( @mysql_query ( $query ) )

        {

          $success = 'Article updated successfully!';

        }

        else {

          die ( mysql_error () );

        }

      }

      else {

        echo 'Please ensure that you have a title and some content for this article!';

      }

    }

  }

  else {

    die ( mysql_error () );

  }

 

 

  if ( isset ( $success ) ) {

    echo $success;

  }

  else {

  //we need this form only if we don't have the success message

?>

  <form id="update" name="update" method="post" action="<?=$_SERVER['PHP_SELF']?>">

    <input type="hidden" name="_submit_check" value="1"/>

    Article title:<br />

    <input name="article_title" type="text" id="article_title" size="55" value="<?php if ( isset ( $_POST['article_title'] ) ){ echo $_POST['article_title']; }else{ echo $row['article_title'];} ?>" />

      <br /><br />

    Article content:<br />

    <textarea name="article_content" cols="55" rows="5" id="article_content"><?php if ( isset ( $_POST['article_content'] ) ){ echo $_POST['article_content']; }else{ echo $row['article_content'];} ?></textarea>

      <br /><br />

    <input type="submit" name="Submit" value="Submit" />

  </form>

<?php } ?>

?>

</body>

 

</html>

Link to comment
https://forums.phpfreaks.com/topic/100404-html-php-mysql-error/
Share on other sites

// connect to the mysql database server.
  $connect = @mysql_connect ($host, $user, $pass);
  $sql = "SELECT * FROM `articles` WHERE `ID` = " . mysql_real_escape_string ( $_GET['ID'] );

  mysql_select_db ( $database, $connect );

 

Surely you need to connect to the SQL server, then select a datbase, and then you can query it. Lose the @ symbols so you don't suppress errors (which are often quite helpfully constructed).

 

// connect to the mysql database server.
$connect = mysql_connect ($host, $user, $pass);
mysql_select_db($database, $connect);
$sql = "SELECT * FROM articles WHERE ID = " . mysql_real_escape_string ( $_GET['ID'] );

Link to comment
https://forums.phpfreaks.com/topic/100404-html-php-mysql-error/#findComment-513521
Share on other sites

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.