Help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jauson
Forum Contributor
Posts: 111
Joined: Wed Oct 05, 2011 12:59 am

Help

Post by jauson »

Hi Please help!

can someone please why this code not working.
<?php
$host="Localhost";
$username="root";
$password="root";
$db_name="bbs";
$tbl_name="t_board";

mysql_connect('Localhost', 'root', 'root') or die("Unable to Connect");
mysql_select_db("bbs") or die("Unable to select DB!");

$index=$_POST['index'];
$writer=$_POST['writer'];
$subject=$_POST['subject'];
$message=$_POST['message'];
$filename=$_POST['filename'];

$sql = "UPDATE INTO t_board
SET writer='$writer', 'subject='$subject', message='$message', 'filename='$filename'
WHERE index='$index'";
$result = mysql_query($sql);

if(!$result){
echo "Query NOT WORKING"; <---- QUERY NOT WORKING
exit;
}

if ($result){
header("Location: view.php");
}
else{
echo "Please Try Again!";
echo "<BR />";
echo "<a href='view.php'> BACK </a>";
}
mysql_close();

?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Help

Post by Celauran »

Please use syntax tags and meaningful titles. UPDATE INTO is not valid SQL syntax. Remove the INTO and try it again.
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: Help

Post by flying_circus »

In addition to what Celauran said, you have incorrect quoting in your query string. Also, the line with <---- QUERY NOT WORKING is a syntax error, you can put a double slash in front of the text to comment it (//<--- QUERY NOT WORKING).

Code: Select all

<?php
  # Database Credentials
    $host = "Localhost";
    $username = "root";
    $password = "root";
    $db_name = "bbs";
    $tbl_name = "t_board";
    
  # Connect to the Database
    mysql_connect($host, $username, $password) or die("Unable to Connect");
    mysql_select_db($db_name) or die("Unable to select DB!");
    
  # Fetch POST Vars
    $index = isset($_POST['index']) ? $_POST['index'] : '';
    $writer = isset($_POST['writer']) ? $_POST['writer'] : '';
    $subject = isset($_POST['subject']) ? $_POST['subject'] : '';
    $message = isset($_POST['message']) ? $_POST['message'] : '';
    $filename = isset($_POST['filename']) ? $_POST['filename'] : '';
    
  # Build SQL Query
    $sql = sprintf("UPDATE `%s` SET `writer`='%s', `subject`='%s', `message`='%s', `filename`='%s' WHERE `index`='%s';",
                   mysql_real_escape_string($tbl_name),
                   mysql_real_escape_string($writer),
                   mysql_real_escape_string($subject),
                   mysql_real_escape_string($message),
                   mysql_real_escape_string($filename),
                   mysql_real_escape_string($index));
                   
  # Execute SQL Query
    $result = mysql_query($sql);
    
  # Close Query
    mysql_close();
    
  # Check Results
    if($result === FALSE) {
      exit("Query NOT WORKING"); //<---- QUERY NOT WORKING
    } else {
      header("Location: view.php");
      exit;
    }
?>
jauson
Forum Contributor
Posts: 111
Joined: Wed Oct 05, 2011 12:59 am

Re: Help

Post by jauson »

thanks man! you really amazing! "flying_circus"

what special function or script did you use?
Post Reply