Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Aug 2002
    Posts
    66
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Database prob - wont update or insert?

    Hello!
    Another problem - and i have had this one for a long time!

    I want to use the code below to edit and add to the news database for our site.
    It just seems that whenever i want to edit or insert news it wont communicate with the database or something.

    When i go to edit a item, all the information will come into the form fields, but just wont update. Can someone please take a look at the code?

    PHP Code:
    mysql_select_db("news",$db);



    if ($submit) {

      // here if no ID then adding else we're editing

      if ($id) {

        $sql = "UPDATE newsdb id='$id',displaydate='$displaydate',displaytitle='$displaytitle',time='$time',by='$by',news='$news',realtitle='$realtitle' WHERE id=$id";

      } else {

        $sql = "INSERT INTO newsdb (id,displaydate,displaytitle,time,by,news,realtitle) VALUES ('$id','$displaydate','$displaytitle','$time','$by','$news','$realtitle')";

      }

      // run SQL against the DB

      $result = mysql_query($sql);

      echo "Record updated/edited!<p>";

    } elseif ($delete) {

        // delete a record

        $sql = "DELETE FROM newsdb WHERE id=$id";    

        $result = mysql_query($sql);

        echo "$sql Record deleted!<p>";

    } else {

      // this part happens if we don't press submit

      if (!$id) {

        // print the list if there is not editing

        $result = mysql_query("SELECT * FROM newsdb",$db);

        while ($myrow = mysql_fetch_array($result)) {

          printf("<a href=%s?id=%s>%s %s</a> \n", $PHP_SELF, $myrow["id"], $myrow["displaytitle"], $myrow["last"]);

          printf("<a href=%22%25s?id=%s&delete=yes/&quot;>(DELETE)</a><br>", $PHP_SELF, $myrow["id"]);

        }

      }



      ?>

      <P>

      <a href="<?php echo $PHP_SELF?>">ADD A RECORD</a>

      <P>

      <form method="post" action="<?php echo $PHP_SELF?>">

      <?php



      
    if ($id) {

        
    // editing so select a record

        
    $sql "SELECT * FROM newsdb WHERE id=$id";

        
    $result mysql_query($sql);

        
    $myrow mysql_fetch_array($result);

        
    $id $myrow["id"];

        
    $displaydate $myrow["displaydate"];

        
    $displaytitle $myrow["displaytitle"];

        
    $time $myrow["time"];

        
    $by $myrow["by"];
        
        
    $news $myrow["news"];

        
    $realtitle $myrow["realtitle"];
        
        
    // print the id for editing

    }

        
    ?>

        <input type=hidden name="id" value="<?php echo $id ?>" size="20">

        <?php

      
    }



      
    ?>


          Post Date :
          <input type="text" name="displaydate" size="26" value="<?php echo $displaydate ?>"><br>
          <br>
          Display Title: <font face="Verdana" color="#999999" size="2">
        


          <input type="text" name="displaytitle" size="26" value="<?php echo $displaytitle ?>"><br>
          <br>
          </font>Time : <font face="Verdana" color="#999999" size="2">
        


          <input type="text" name="time" size="26" value="<?php echo $time ?>"><br>
          <br>
          </font>By :<font face="Verdana" color="#999999" size="2">
        


          <input type="text" name="by" size="26" value="<?php echo $by ?>"><br>
          <br>
          </font>News Bulk : <font face="Verdana" color="#999999" size="2">
        


          <textarea rows="9" name="news" cols="41"><?php echo $news ?></textarea><br>
          <br>
          <br>
          </font>Real Title: <font face="Verdana" color="#999999" size="2">
        


          <input type="text" name="realtitle" size="26" value="<?php echo $realtitle ?>"></font></font><font face="Verdana" color="#999999" size="2"><font face="Verdana" size="2"><br>
          <br>
          <br>
          </font><br>
          <input type="Submit" name="submit" value="Enter information">

        </form>
    Thankyou!!!

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    I see you selecting the db

    mysql_select_db("news",$db);


    but I don't see the db handle with the connection.

    should look something like this:

    $db = @mysql_connect("localhost", "dbusername", "dbpassword") or die("Unable to connect to database at this time");
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    New Coder
    Join Date
    Aug 2002
    Posts
    66
    Thanks
    2
    Thanked 0 Times in 0 Posts
    It is there - I just didn't copy it into the post - didn't think it was neccessary.. Sorry!

    Anymore suggestions?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •