Jump to content

Please Help With Editing MySQL


Spartan 117

Recommended Posts

Hello, I have a problem, I am trying to make a page that edits multiple thing on my website. I have a php website that retrieves text from mysql. I have the page code below:

[code]
          <?php
//initilize PHP
include "config.php";


if($_POST['submit']) //If submit is hit
{
// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());

// select the database
mysql_select_db($database)
or die ("Could not select database because ".mysql_error());

  //convert all the posts to variables:
  $text = $_POST['text'];
  $url = $_POST['url'];
 
  //Insert the values into the correct database with the right fields
  //mysql table = links
  //table columns = id, text, url
  //post variables = $text, $url
  $result=MYSQL_QUERY("INSERT INTO links (id,text,url)".
      "VALUES ('NULL', '$text', '$url')");

    //confirm
  echo "Link Added";
  echo '<br> <a href="admin.php">Click Here To Go Back</a>';
}
else
{
// close php so we can put in our code
php?>
        </p>
        <form method="post" action="edit.php">
          <div align="center">
            <table>
              <tr>
                <td><strong>Text:</strong></td>
                <td><input name='text' type='text' id="text" size="30" /></td>
              </tr>
              <tr>
                <td><strong>URL:</strong></td>
                <td><input name='url' type='text' id="url" value='' size="30" /></td>
              </tr>
              <br />
              <tr>
                <td></td>
                <br />
                <td><input type="submit" name="submit" value="Add Student Page" /></td>
              </tr>
            </table>
          </div>
        </form>
        <div align="center">
          <?php
} //close the else statement
php?>
        </div></p>
        <p align="center"><strong>Edit Link:</strong></p>
        <div align="center">
          <?php
include "config.php";
// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());

// select the database
mysql_select_db($database)
or die ("Could not select database because ".mysql_error());

//If cmd has not been initialized
if(!isset($cmd))
{
  //display all the links
  $result = mysql_query("select * from $table order by id desc");
 
  //run the while loop that grabs all the news scripts
  while($r=mysql_fetch_array($result))
  {
      //grab the title and the ID of the news
      $name=$r["text"];//take out the title
      $id=$r["id"];//take out the id
   
//make the title a link
      echo "<a href='edit.php?cmd=edit&id=$id'>$name</a>";
      echo "<br>";
    }
}
php?>
          <?php
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
  if (!isset($_POST["submit"]))
  {
      $id = $_GET["id"];
      $sql = "SELECT * FROM $table WHERE id=$id";
      $result = mysql_query($sql);       
      $myrow = mysql_fetch_array($result);
      php?>
          <form action="edit.php" method="post">
            <table width="200" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td><input type="hidden" name="id" value="<?php echo $myrow["id"] ?>" />
                  <strong>Text:</strong> </td>
                <td><input name="name" type="text" id="name" value="<?php echo $myrow["text"] ?>" size="30" /></td>
              </tr>
              <tr>
                <td><strong>URL:</strong></td>
                <td><input name="url2" type="text" id="url2" value="<? echo $myrow["url"] ?>" size="30" /></td>
              </tr>
              <tr>
                <td><input type="hidden" name="cmd" value="edit" /></td>
                <td><input type="submit" name="submit2" value="Edit Student Page" /></td>
              </tr>
            </table>
          </form>
          <?php } php?>
          <?php
  if ($_POST["submit2"])
  {
          $name = $_POST["text"];
  $url = $_POST["url"];  
  $sql = "UPDATE $table SET text='$name',url='$url' WHERE id=$id";
      //replace link with your table name above
      $result = mysql_query($sql);
      echo "Link Updated!";
}
}
php?>
[/code]

The first part works and adds the link, but the second part won't edit the link, and I get this message: [quote]Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\AppServ\www\origin\edit.php on line 188[/quote]

I don't understand what is wrong. Could it be that I have defined a certain (string? $whatever = something) more than once?

I was going to try to make it so when you are on the page in th beginning it would have links on what to change and then the php would echo the tables or whatever. Would I do that with the cmd thing? I saw that a few times and it looks like it is for changing the content on the same page.

Thanks
Link to comment
https://forums.phpfreaks.com/topic/29980-please-help-with-editing-mysql/
Share on other sites

I'm sorry, I found the problem, I had pasted the form into Dreamweaver and it renamed the the button to "submit2" because "submit" already existed... So I fixed the code to say "submit2" and it works now.

But how would I do what I said before:

[quote author=Spartan 117 link=topic=117910.msg481352#msg481352 date=1165613508]
I was going to try to make it so when you are on the page in th beginning it would have links on what to change and then the php would echo the tables or whatever. Would I do that with the cmd thing? I saw that a few times and it looks like it is for changing the content on the same page.
[/quote]

Thanks

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.