Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 375
  • Last Modified:

checkboxes inserting data into MySQL

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
qwerty567
Asked:
qwerty567
  • 9
  • 6
1 Solution
 
jayrodCommented:
what query does query2 create when you output it?

i.e. var_dump($query2);

This is the first place you should look at..
0
 
qwerty567Author Commented:
var_dump($query2);

ouputs

NULL
0
 
WysGCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
qwerty567Author Commented:
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
 
WysGCommented:
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
 
qwerty567Author Commented:
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
 
WysGCommented:
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
 
qwerty567Author Commented:
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
 
qwerty567Author Commented:
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
 
WysGCommented:
And if you erase the echo $query2;
Does it still work ?
0
 
qwerty567Author Commented:
yes it does. so what did we change that made it work?
0
 
WysGCommented:
Can you copy paste your complete code please, to see where you did change
0
 
qwerty567Author Commented:
yes it does. so what did we change that made it work?
0
 
qwerty567Author Commented:
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
 
WysGCommented:
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
 
qwerty567Author Commented:
thanks for all your help
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 9
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now