Page 1 of 1

Drop-downs and MySQL

Posted: Sat Dec 10, 2005 12:33 pm
by LiveFree
Hello, say I have this code...

Code: Select all

<?php
include('includes/connect.php');

if (isset($_POST['submitted'])){
  if (isset($_POST['id'])){
    if(isset($_POST['name'])){
      $id=$_POST['id'];
      $name=$_POST['name'];
      $sql="INSERT INTO test (id, name) VALUES ('$id','$name')";
      $result=mysql_query($sql);
      if (mysql_affected_rows($result) == 1){
        echo "The script has completed sucessfully!";
        mysql_close();
      }else{
        echo "There seems to be an issue.";
      }
    }else{
      echo "Please select your name";
    }
  }else{
    echo "Please select your id";
  }
}else{
  echo '<form name="test" method="POST" action="test.php">
  ID:<select name="id"><option value="1">1</option><option value="2">2</option></select>
  <br>Name: <select name="name"><option value="Tucker">Tucker</option><option name="Haener">Haener</option></select><br>
  <input type="submit" name="submit" value="Submit"><input type="hidden" name="submitted" value="TRUE"></form>';
}
http://www.aa-25thID.com/ATFCS/test.php is the link if you want to try it

When you do it, it says:
Warning: mysql_affected_rows(): supplied argument is not a valid MySQL-Link resource in /home/recon/public_html/ATFCS/test.php on line 11
There seems to be an issue.

I was just wondering if I am not follwing the syntax for inserting <option> values into mysql....

THanks for any and all help :)

Posted: Sat Dec 10, 2005 12:38 pm
by twigletmac
The first thing to do is to add some error handling when you run the query. So instead of:

Code: Select all

$result=mysql_query($sql);
try

Code: Select all

$result = mysql_query($sql) or die(mysql_error().'<br />'.$sql);
This will give you a clearer idea as to what's going wrong.

Mac

Posted: Sat Dec 10, 2005 1:04 pm
by LiveFree
Okay I did it but it always gives me the..: Warning: mysql_affected_rows(): supplied argument is not a valid MySQL-Link resource in /home/recon/public_html/ATFCS/test.php on line 11


Could I contact you on MSN to furthur help resolve this issue?

Posted: Sat Dec 10, 2005 1:12 pm
by shiznatix
it will be better if you just use these forums to help other people who read posts to learn things.

the reason you get that error is because there is somthing wrong with that query, you have to do error checking on it.

echo out your $sql and tell us what it says

Posted: Sat Dec 10, 2005 1:13 pm
by twigletmac
From your test script it looks like the problem is that the record already exists:

Code: Select all

Duplicate entry '2-Haener' for key 1
INSERT INTO test (id, name) VALUES ('2','Haener')
if ID is an auto-increment field then you won't need to set it manually each time.

Mac

Posted: Sat Dec 10, 2005 1:14 pm
by LiveFree
INSERT INTO test (id, name) VALUES ('2','Haener')Duplicate entry '2-Haener' for key 1
INSERT INTO test (id, name) VALUES ('2','Haener')

Posted: Sat Dec 10, 2005 1:16 pm
by LiveFree
Aha!

Problem solved

Was an issue with the mysql_affected_rows

Thanks!