?
Solved

checkboxes inserting data into MySQL

Posted on 2003-03-20
16
Medium Priority
?
366 Views
Last Modified: 2013-12-12
Hi

I am trying to use checkboxes generated from MySQL db and then insert what is checked into db.I am following a tutorial from php builder but i can't get it to work. It displays the checkboxes fine but won't insert anything into the db. Can anyone help. The tutorial at php is located http://www.phpbuilder.com/columns/laflamme20001016.php3

I have two files checkbox.php and  interskills.php and i think the problem is with interskills.php. I don't get any error messages just a my display "The info was not inserted!"

checkbox.php
--

<?php
/* get the checkbox labels */
  $skills = get_checkbox_labels("const_skills");
 
  /* create the html code for a formatted set of
     checkboxes */
  $html_skills = make_checkbox_html($skills, 3, 400, "skills[]");
?>



<html>
<body>
<br>
<form name="skills" method="POST" action="insertskills.php">
   Check off the subjects that you are enrolled in:

   <? echo "$html_skills"; ?>
   <br>
   <input type="submit" value="Submit">
</form>
</body>
</html>


<?php


function get_checkbox_labels($table_name) {
// set up variables for the database
$host = "localhost";
$user = "username";
$password = "password";
$dbname = "testing";

// establish connection to the database
$link = @mysql_connect($host, $user, $password) or die("Cant connect to server");

  /* make an array */
$arr = array();
$query = "SELECT * FROM $table_name";
$result = mysql_db_query ($dbname, $query, $link);
$num_rows=@mysql_num_rows($result);

while ($row = mysql_fetch_object($result)) {
    array_push($arr, $row);

}
  return $arr;

}


/* Prints a nicely formatted table of checkbox choices.
   
   $arr is an array of objects that contain the choices
   $num is the number of elements wide we display in the table
   $width is the value of the width parameter to the table tag
   $name is the name of the checkbox array
   $checked is an array of element names that should be checked
*/  
function make_checkbox_html($arr, $num, $width, $name, $checked=array()) {
// set up variables for the database
$host = "localhost";
$user = "username";
$password = "password";
$dbname = "testing";

// establish connection to the database
$link = mysql_connect ($host, $user, $password);
 
  /* create string to hold out html */
  $str = "";
 
  /* make it */
  $str .= "<table width=\"$width\" border=\"1\">\n";
  $str .= "<tr>\n";
  /* determine if we will have to close add
     a closing tr tag at the end of our table */
  if (count($arr) % $num != 0) {
    $closingTR = true;
  }
 
  $i = 1;
  if (isset($checked)) {
    /* if we passed in an array of the checkboxes we want
       to be displayed as checked */
    foreach ($arr as $ele) {
      $str .= "<td><input type=\"checkbox\" name=\"$name\" value=\"$ele->id\"";
      foreach ($checked as $entry) {
    if ($entry == $ele->id) {
      $str .= "checked";
          continue;
        }
      }
      $str .= ">";
      $str .= "$ele->value";
      if ($i % $num == 0) {
        $str .= "</tr>\n<tr>";
      } else {
        $str .= "</td>\n";
      }
      $i++;
    }
 
  } else {
    /* we just want to print the checkboxes. none will have checks */
    foreach ($arr as $ele) {
      $str .= "<td><input type=\"checkbox\" name=\"$name\" value=\"$ele->id\">";
      $str .= "$ele->value";
     
      if ($i % $num == 0) {
        $str .= "</tr>\n<tr>";
      } else {
        $str .= "</td>\n";
      }
      $i++;
    }
 
  }
  /* tack on a closing tr tag if necessary */
  if ($closingTR == true) {
    $str .= "</tr></table>\n";
  } else {
    $str .= "</table>\n";
  }
  return $str;
}
?>


interskills.php
--
<?php
// set up variables for the database
$host = "localhost";
$user = "username";
$password = "password";
$dbname = "testing";
$tableName = "lookup_skills";
$uid = "1";

// establish connection to the database
$link = @mysql_connect($host, $user, $password) or die("Cant connect to server");

$query = "DELETE from $tableName WHERE (uid = '$uid')";
if (mysql_db_query ($dbname, $query, $link))
{
print ("The info has been deleted from the database.<br>");
} else {
print ("The info was not deleted!<BR>");
}

$query2 = create_checkbox_query($skills, "lookup_skills", $uid);
if (mysql_db_query ($dbname, $query2, $link))
{
print ("The comments been inserted from the database.<br>");
} else {
print ("The info was not inserted!<BR>");
}

function create_checkbox_query($arr, $table, $uid) {
$q = "INSERT into $table (uid, skill_id) VALUES";
   
    foreach ($arr as $check) {
            $q .= " ( $uid , $check ) " . ",";
//$q .= ",";
      }
       return substr($q, 0, -1);
}
?>

0
Comment
Question by:qwerty567
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 6
16 Comments
 
LVL 3

Expert Comment

by:jayrod
ID: 8174707
what query does query2 create when you output it?

i.e. var_dump($query2);

This is the first place you should look at..
0
 

Author Comment

by:qwerty567
ID: 8177397
var_dump($query2);

ouputs

NULL
0
 
LVL 3

Expert Comment

by:WysG
ID: 8178351
To find where the error is the tip is to do that :

[...]
$result = mysql_db_query ($dbname, $query, $link) or die("[Err] Error with this query : $query".mysql_error()); //I've Added the or die here
[...]
$link = mysql_connect ($host, $user, $password) or die("Cant connect to server".mysql_error()); //I've Added the or die here
[...]
if (mysql_db_query ($dbname, $query, $link) or die("[Err] Error with this query : $query".mysql_error()) ) //Not sure about this one, haven't tested it, but you get the idea
[...]
$query2 = create_checkbox_query($skills, "lookup_skills", $uid);
if (mysql_db_query ($dbname, $query2, $link) or die("[Err] Error with this query : $query".mysql_error()))
[...]
0
WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

 

Author Comment

by:qwerty567
ID: 8178579
I put in

if (mysql_db_query ($dbname, $query2, $link) or die("[Err] Error with this query : $query2".mysql_error()))

and get

[Err] Error with this query : Query was empty

why is it empty?
0
 
LVL 3

Expert Comment

by:WysG
ID: 8183219
The problem may be in the create_checkbox_query function

Try this

function create_checkbox_query($arr, $table, $uid)
{
     echo "<pre>";
     echo "arr<br>";
     print_r($arr);
     echo "table<br>";
     print_r($table);
     echo "uid<br>";
     print_r($uid);
     echo "</pre>";

     $q = "INSERT into $table (uid, skill_id) VALUES";
     echo $q."<br>";
 
     foreach ($arr as $check) {
          $q .= " ( $uid , $check ) " . ",";
          echo $q."<br>";
     }
     echo substr($q, 0, -1);
     return substr($q, 0, -1);
}

With this you should be able to find what is the probleme with your query
0
 

Author Comment

by:qwerty567
ID: 8184543
This is my output now
--
The info has been deleted from the database.

arr
Array
(
    [0] => 1
    [1] => 4
)
table
lookup_skillsuid
1
INSERT into lookup_skills (uid, skill_id) VALUES
INSERT into lookup_skills (uid, skill_id) VALUES ( 1 , 1 ) ,
INSERT into lookup_skills (uid, skill_id) VALUES ( 1 , 1 ) , ( 1 , 4 ) ,
INSERT into lookup_skills (uid, skill_id) VALUES ( 1 , 1 ) , ( 1 , 4 ) [Err] Error with this query : Query was empty

I'm not really sure what all that means. Can you explain. It still isn't inserting into the database.
0
 
LVL 3

Expert Comment

by:WysG
ID: 8184655
Ok so eveything seems to be alright in the function

Try
$query2 = create_checkbox_query($skills, "lookup_skills", $uid);
echo $query2; // Maybe the variable is not set to the proper value
if (mysql_db_query ($dbname, $query2, $link) or die("[Err] Error with this query : $query2".mysql_error()))
0
 

Author Comment

by:qwerty567
ID: 8184702
Its working but i don't understand why

isn't the only change we made last time around

echo $query2;  

how did that make it work?
0
 

Author Comment

by:qwerty567
ID: 8184733
Its working but i don't understand why

isn't the only change we made last time around

echo $query2;  

how did that make it work?
0
 
LVL 3

Expert Comment

by:WysG
ID: 8184809
And if you erase the echo $query2;
Does it still work ?
0
 

Author Comment

by:qwerty567
ID: 8184838
yes it does. so what did we change that made it work?
0
 
LVL 3

Expert Comment

by:WysG
ID: 8184867
Can you copy paste your complete code please, to see where you did change
0
 

Author Comment

by:qwerty567
ID: 8184875
yes it does. so what did we change that made it work?
0
 

Author Comment

by:qwerty567
ID: 8184883
sorry for the double up comments. don't know what is going on there. Here is my code for insertskills.php

<?php
// set up variables for the database
$host = "localhost";
$user = "username";
$password = "passwrod";
$dbname = "dbname";
$tableName = "lookup_skills";
$uid = "1";

// establish connection to the database
$link = @mysql_connect($host, $user, $password) or die("Cant connect to server");

$query = "DELETE from $tableName WHERE (uid = '$uid')";
if (mysql_db_query ($dbname, $query, $link))
{
print ("The info has been deleted from the database.<br>");
} else {
print ("The info was not deleted!<BR>");
}

$query2 = create_checkbox_query($skills, "lookup_skills", $uid);

if (mysql_db_query ($dbname, $query2, $link) or die("[Err] Error with this query : $query2".mysql_error()))
{
print ("The data has been inserted in the database.<br>");
} else {
print ("The info was not inserted!<BR>");
}

function create_checkbox_query($arr, $table, $uid) {
$q = "INSERT into $table (uid, skill_id) VALUES";
foreach ($arr as $check) {
     $q .= " ( $uid , $check ) " . ",";
}
//echo substr($q, 0, -1);
return substr($q, 0, -1);
}
?>

0
 
LVL 3

Accepted Solution

by:
WysG earned 500 total points
ID: 8185002
It look's like it is the same code but with some "or die("Bleh") in addition ... I can't see why it wasn't working and then started to work.

So you know what this mean? It mean that the error is almost stupid too stupid to be see, but it's those stupid error that we just can't see that can pissed you off ^_^
0
 

Author Comment

by:qwerty567
ID: 8185014
thanks for all your help
0

Featured Post

WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question