Jump to content

Updating a DB with a form.


Lee-Bartlett

Recommended Posts

I followed this TUT online and i cant see what i got wrong,

 

  <?php require_once("includes/db_connection.php") ?>
  
  <?php 
//If cmd has not been initialized

if(!isset($cmd)) 
{
   //display all the business

   $result = mysql_query("select * from tblbasicform by email"); 

   

   //run the while loop that grabs all the business scripts

   while($r=mysql_fetch_array($result));

   { 

      //grab the business_name and the username of the business

      $email=$r["email"];//take out the business_name

      $name=$r["name"];//take out the username

     

     //make the business_name a link

    echo "<a href='edit.php?cmd=edit&username=$username'>$business_name - Edit</a>";

     // echo "<br>";

    }

}

?>





<?

if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")

{

   if (!isset($_POST["submit"]))

   {

      $name= $_GET["name"];

      $sql = "SELECT * FROM tblbasicform WHERE name='$name'";

      $result = mysql_query($sql);        

      $myrow = mysql_fetch_array($result);

      ?>

      

      <form action="edit.php" method="post">

      <input type=hidden name="username" value="<?php echo $myrow["name"] ?>">

   

      business_name:<INPUT TYPE="TEXT" NAME="email" VALUE="<?php echo $myrow["email"] ?>" SIZE=30><br>

      info:<TEXTAREA NAME="locaiton" ROWS=10 COLS=30><? echo $myrow["location"] ?></TEXTAREA><br>

      industry:<INPUT TYPE="TEXT" NAME="buissnes_name" VALUE="<?php echo $myrow["buissnes_name"] ?>" SIZE=30><br>

   

      <input type="hidden" name="cmd" value="edit">

   

      <input type="submit" name="submit" value="submit">

   

      </form>

   

   <? } ?>

  
   

  <?

   if ($_POST["$submit"])

   {

      $email = $_POST["email"];

      $location = $_POST["location"];

      $buissnes_name = $_POST["buissnes_name"];



      $sql = "UPDATE tblbasicform SET email='$email',location='$location', buissnes_name='$buissnes_name' WHERE name='$name'";



      $result = mysql_query($sql);

      echo "Thank you! Information updated.";

   }

}

?>

 

i get this error

 

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\Website\attempt.php on line 16

 

Link to comment
https://forums.phpfreaks.com/topic/126175-updating-a-db-with-a-form/
Share on other sites

<?php 
$db_host = "localhost"; // Your database host server, eg. db.server.com
$db_user = "takenout"; // User who has access to the database
$db_pass = "takenout"; // User password to access database
$db_name = "roberts_work"; // Existing database name

// -- Connecting to the database (not persistent connection)

$connect = mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_name,$connect) or die(mysql_error());


?>

Ah, the problem seems to be in your query:

<?php $result = mysql_query("select * from tblbasicform by email");

I think you want 'ORDER BY email'.

<?php $result = mysql_query("select * from tblbasicform ORDER BY email") or die(mysql_error());

also adding the die after will show you if there is a problem in your query...

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.