Jump to content

[SOLVED] Code won't work?


ItsWesYo

Recommended Posts

My friend created an "add" and "edit" news function for me. The pre-problem is that when I add the news and click submit, it says it was added to the mysql database successfully. The real problem is that the data doesn't show up in the mysql database. As it was a quick code for him, it probably has an error in it somewhere, but I can't find it.

 

<b>add_news.php:</b>

<? 
include ("http://www.1.projectwes.hyspex.com/includes/header.php");

if($_POST['submit']) //If submit is hit
{
   mysql_connect("localhost","itswesyo","pass"); 
   mysql_select_db("itswesyo_news"); 
   $id = $_POST['id'];
   $title = $_POST['title'];
   $message = $_POST['message'];
   $who = $_POST['who'];
   $date = $_POST['date'];
   
   $result=MYSQL_QUERY("INSERT INTO news (id,title,message,who,date)".
      "VALUES ('NULL', '$title', '$message', '$who', '$date')"); 

    //confirm
   echo "The news was added successfully."; 
}
else
{
// close php so we can put in our code
?>

<form method="post" action="http://www.1.projectwes.hyspex.com/staff/add_news.php">

<b>Title:</b><br>
<input type='text' name='title' size='25' class='news_box'><br><br>

<b>Author:</b><br>
<select name='who' class='news_box'>
<option value='Wes'>Wes
</select><br><br>

<b>Date:</b><br>
<input type='text' name='date' class='news_box' value='<? echo date("m/d/y"); ?>' size='25'><br><br>

<b>Message:</b><br>
<textarea name='message' cols='50' rows='10' class='news_box'></textarea><br><br>

<input type='submit' name='submit' value='Add news' class='news_submit'>

</form>

<?
} //close the else statement
include ("http://www.1.projectwes.hyspex.com/includes/footer.php");
?>

 

 

edit_news.php:

<? 
include ("http://www.1.projectwes.hyspex.com/includes/header.php");

mysql_connect("localhost","itswesyo","password"); 
mysql_select_db("itswesyo_news"); 

if(!isset($cmd)) 
{
   $result = mysql_query("SELECT * FROM news order by id"); 
   while($r=mysql_fetch_array($result)) 
   { 
      //grab the title and the ID of the news
      $title=$r["title"];//take out the title
      $id=$r["id"];//take out the id
     
      echo "<a href='http://www.1.projectwes.hyspex.com/staff/edit_news.php?cmd=edit&id=$id'>$title - Edit</a>";
      echo "<br>";
    }
}
?>
<?
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
   if (!isset($_POST["submit"]))
   {
      $id = $_GET["id"];
      $sql = "SELECT * FROM news WHERE id=$id";
      $result = mysql_query($sql);        
      $myrow = mysql_fetch_array($result);
      ?>
  
      <form action="http://www.1.projectwes.hyspex.com/staff/edit_news.php" method="post">
      <input type=hidden name="id" value="<?php echo $myrow["id"] ?>">
   
      Title:<INPUT TYPE="TEXT" NAME="title" VALUE="<?php echo $myrow["title"] ?>" SIZE=30><br>
      Message:<TEXTAREA NAME="message" ROWS=10 COLS=30><? echo $myrow["message"] ?></TEXTAREA><br>
      Who:<INPUT TYPE="TEXT" NAME="who" VALUE="<?php echo $myrow["who"] ?>" SIZE=30><br>
   
      <input type="hidden" name="cmd" value="edit">
   
      <input type="submit" name="submit" value="submit">
   
      </form>
   
<? } ?>
<?
   if ($_POST["$submit"])
   {
      $title = $_POST["title"];
  $message = $_POST["message"];
  $who = $_POST["who"];
  
  $sql = "UPDATE news SET title='$title',message='$message',who='$who' WHERE id=$id";
      //replace news with your table name above
      $result = mysql_query($sql);
      echo "Thank you! Information updated.";
}
}
include ("http://www.1.projectwes.hyspex.com/includes/footer.php");
?>

Link to comment
https://forums.phpfreaks.com/topic/36943-solved-code-wont-work/
Share on other sites

Try:

 

<?php

mysql_connect("localhost","itswesyo","pass") or die(mysql_error()); 
mysql_select_db("itswesyo_news") or die(mysql_error()); 
$id = $_POST['id'];
$title = $_POST['title'];
$message = $_POST['message'];
$who = $_POST['who'];
$date = $_POST['date'];

$result = mysql_query("INSERT INTO news (id,title,message,who,date) VALUES ('NULL', '$title', '$message', '$who', '$date')") or die(mysql_error());

if(mysql_affected_rows())
  echo "The news was added successfully.";
else
  echo "<br>Failed";

?>

 

Orio.

Link to comment
https://forums.phpfreaks.com/topic/36943-solved-code-wont-work/#findComment-176268
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.