Hello
Can someone please check my code snippet below (only part of code included!). PHP form that checks $name doesn't already exist and then inserts them into mySQL database. All works except for the $counted variable. $list is a string that looks something like this: 123,2536,2346,332,546. So in this instance $counted should have a value of 5 because there are 5 values in the string. But it gets inserted as 0. Cannot see what I am doing wrong.

Many thanks!

  if (isset($_POST['submit'])) {
     $name=$HTTP_POST_VARS['name'];
     $passw=$HTTP_POST_VARS['passw'];
     $list=$HTTP_POST_VARS['list'];
     $plot=$HTTP_POST_VARS['plot'];

if ($name && $passw && $list && $plot) {

$list_array = explode (',',$_POST['list']);
$string_list = implode (',',$list_array);


$sql = "SELECT id FROM table WHERE id IN($string_list)";
$result=mysql_query($sql); 
$counted = mysql_num_rows($result);


$qstr = mysql_query("select id FROM table WHERE name='$name'")
          or die ("Could not log you in."); 
if (mysql_num_rows($qstr) == 0) {
     $query ="INSERT INTO table (name,passw,list,plot,counted)";
     $query.=" VALUES ('$name','$passw','$list','$plot','$counted')";
     $result=mysql_query($query);
     if ($result) {
header("Location: http://...whatever...");

    (why are you mixing $_POST and http_post_vars?)

    Anyway, can't see why you are going through explode/implode if you are posting the list. Why not just use what is posted?

    DEBUG: echo the list and print_r the exploded array to se what is in them.
    Echo your query string to see what it says
    Your count is zero because the query is retruning no rows, so either the query string is wrong or the ids do not exist.

    In any case you could get the counted from count($list_array) if they do all exist.

      I had taken the explode/implode from another page where I print out the value.

      It was a bad recycling job.

      So count() did the job prefectly.

      Thank you so much.

        Write a Reply...