Drop-downs and MySQL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
LiveFree
Forum Contributor
Posts: 258
Joined: Tue Dec 06, 2005 5:34 pm
Location: W-Town

Drop-downs and MySQL

Post 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 :)
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
LiveFree
Forum Contributor
Posts: 258
Joined: Tue Dec 06, 2005 5:34 pm
Location: W-Town

Post 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?
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
LiveFree
Forum Contributor
Posts: 258
Joined: Tue Dec 06, 2005 5:34 pm
Location: W-Town

Post 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')
LiveFree
Forum Contributor
Posts: 258
Joined: Tue Dec 06, 2005 5:34 pm
Location: W-Town

Post by LiveFree »

Aha!

Problem solved

Was an issue with the mysql_affected_rows

Thanks!
Post Reply