Results 1 to 4 of 4

Thread: Update MYSQL Rows

  1. #1
    Join Date
    Feb 2009
    Posts
    303
    Thanks
    18
    Thanked 36 Times in 36 Posts

    Default Update MYSQL Rows

    Hi All,

    I want to have a simple two-field form update a table row of two values.

    So far it'll read the table, but I can't get it to update the table. I'm pretty new to PHP, but can read it better than I can code it...

    I have two pages, the form page (index.php) and the output (page.php), index.php holds the form, and posts the form data to page.php.

    page.php holds the following: (I removed the username/password from this post)
    Code:
    <?php
    $con = mysql_connect("localhost","My Username","My Password");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("learningphp_cms", $con);
    ?>
    
    <?php
    $result = mysql_query("SELECT * FROM page1");
    $chickenpie = mysql_query("SELECT title FROM page1");
    $beefstew = mysql_query("SELECT bodytext FROM page1");
    
    while($row = mysql_fetch_array($result))
      {
      echo "<h1>" . $row['title'] . "</h1> <p>" . $row['bodytext'] . "</p>";
      echo "<br />";
      }
    $sql = 'UPDATE `my database`.`page1` SET `title` = $_POST[\'title\'], `bodytext` = $_POST[\'bodytext\'] WHERE CONVERT(`page1`.`title` USING utf8) = mysql_fetch_array($chickenpie) AND CONVERT(`page1`.`bodytext` USING utf8) = mysql_fetch_array($beefstew) LIMIT 1;';
    ?>
    And index.php holds this: (I removed the styles)
    Code:
    <form action="page.php" method="post">
    	<h1>Website Title</h1>
    	<input type="text" name="title" /><br/><br/>
    	<h1>Site Content (Body Text)</h1>
    	<textarea name="bodytext"></textarea><br/>
    	<input type="submit" value="Update Page" name="submit" style="font-size:25px; padding:0 10px 0 10px;" />
    </form>
    If anyone could help, it would be greatly appreciated!

    Thanks,
    <X96 Web Design>
    Alex Blackie, X96 Design
    My Website
    I specialize in: HTML5, CSS3, PHP, Ruby on Rails, MySQL, MongoDB, Linux Server Administration

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Try:
    PHP Code:
    <?php
    $con 
    mysql_connect("localhost","My Username","My Password");
    if (!
    $con)
      {
      die(
    'Could not connect: ' mysql_error());
      }

    mysql_select_db("learningphp_cms"$con);
    ?>

    <?php
    $result 
    mysql_query("SELECT * FROM page1");
    $chickenpie mysql_fetch_array(mysql_query("SELECT title FROM page1"));
    $beefstew mysql_fetch_array(mysql_query("SELECT bodytext FROM page1"));

    while(
    $row mysql_fetch_array($result))
      {
      echo 
    "<h1>" $row['title'] . "</h1> <p>" $row['bodytext'] . "</p>";
      echo 
    "<br />";
      }
    //$sql = 'UPDATE `my database`.`page1` SET `title` = $_POST[\'title\'], `bodytext` = $_POST[\'bodytext\'] WHERE CONVERT(`page1`.`title` USING utf8) = mysql_fetch_array($chickenpie) AND CONVERT(`page1`.`bodytext` USING utf8) = mysql_fetch_array($beefstew) LIMIT 1;';

    mysql_query("UPDATE `my datanase.`page1` SET `title` = '{$_POST['title']}', `bodytext` = '{$_POST['bodytext']}' WHERE CONVERT(`page`.title USING utf8) = '{$chickenpie}' AND CONVERT(`page1`.`bodytech` USING utf8) = '{$beefstew}' LIMIT 1") or die(mysql_error());
    ?>
    Jeremy | jfein.net

  3. #3
    Join Date
    Feb 2009
    Posts
    303
    Thanks
    18
    Thanked 36 Times in 36 Posts

    Default

    I got this:
    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 ' SET `title` = '', `bodytext` = '' WHERE CONVERT(`page`.title USING utf8) = 'Res' at line 1
    Could it be the free hosting I'm using? I really wish I had an old computer kicking around I could use for a server...
    Last edited by X96 Web Design; 04-14-2009 at 03:37 AM. Reason: didn't put the quote in properly...
    Alex Blackie, X96 Design
    My Website
    I specialize in: HTML5, CSS3, PHP, Ruby on Rails, MySQL, MongoDB, Linux Server Administration

  4. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Try:
    Code:
    mysql_query("UPDATE `my datanase.`page1` SET `title` = '".$_POST["title"]."', `bodytext` = '".$_POST["bodytext"]."' WHERE CONVERT(`page`.title USING utf8) = '".$chickenpie."' AND CONVERT(`page1`.`bodytech` USING utf8) = '".$beefstew."' LIMIT 1") or die(mysql_error());
    ?>
    Jeremy | jfein.net

Bookmarks

Posting Permissions

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