Forum Moderators: coopster

Message Too Old, No Replies

Insert multiple records based on checkboxes in dreamweaver

         

lwaters

7:02 pm on Oct 3, 2011 (gmt 0)

10+ Year Member



I am creating a scholarship application selection tool where students can select multiple scholarships to apply to. They review the list of scholarships and check off the ones they want to apply to. I need this data to insert into the same table. The ID is the scholarship ID and UID is the student ID. I am creating this in PHP using Dreamweaver. The following is what I have but, it is not working. Any suggestions would be great. Thanks

THE CODE:

mysql_select_db($database_connect, $connect);
$query_your_scholarships = "SELECT * FROM scholarships ORDER BY NAME ASC";
$your_scholarships = mysql_query($query_your_scholarships, $connect) or die(mysql_error());
$row_your_scholarships = mysql_fetch_assoc($your_scholarships);
$totalRows_your_scholarships = mysql_num_rows($your_scholarships);

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
$valuesSets = "";
for ($i=0; $i < count($ID); $i++) {
$valuesSets.="(".GetSQLValueString($UID[$i], "int").",".GetSQLValueString($ID[$i], "int")."),";
$valuesSets=substr($valuesSets,0,-1); // to remove last comma
$insertSQL = sprintf("INSERT INTO stu_schol (`UID`, `ID`) VALUES %s", $valuesSets);


mysql_select_db($database_connect, $connect);
if ($valuesSets <> "") {
// echo $insertSQL;
$Result1 = mysql_query($insertSQL, $connect) or die(mysql_error());

$insertGoTo = "main_page.php?UID=" . $_GET['UID'] . "";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}}

THE FORM:

<form method="POST" action="<?php echo $editFormAction; ?>" name="standardapps">
<input name="ID[]" type="checkbox" id="ID{}" value="<?php echo $row_your_scholarships['ID']; ?>" />
Add</label>
<input name="UID[]" type="hidden" id="UID{}" value="<?php echo $_GET['UID']; ?>" />
<input type="submit" class="btn" name="submit" id="submit" value="Update My Selections" />
</form>

Wittner

11:46 am on Oct 6, 2011 (gmt 0)

10+ Year Member



What exactly isn't working? Are you getting any error messages? What are the 'symptoms'?

cheers,

Mike

coopster

11:48 am on Oct 6, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, lwaters

The following is what I have but, it is not working.


That could mean many things. The database connection is not working? The query is failing? You can dump the query to the browser to inspect it. Also, during development if you turn up your error_reporting you can show the errors on your display. If this is not possible you can always write them to your logs and check the logs instead.

lwaters

12:12 pm on Oct 6, 2011 (gmt 0)

10+ Year Member



The error message is:

Notice: Undefined variable: ID in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\Scholarship\scholarship_selection.php on line 42

This is line 42: for ($i=0; $i < count($ID); $i++) {

When I check off some of the boxes and hit submit, the page just reloads.

I think I might not be naming some of the field correctly or the coding is off. In the MySQL database, the field names are ID and UID. The checkbox name is ID and the checked value is pulling the ID # from a recordset. Any ideas?

Thanks so much

rocknbil

4:35 pm on Oct 6, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Where is $ID set?

You probably have something like

$ID = $_GET['ID']; // Or $_POST['ID'], depending on form action

Which is a bit insecure (uncleansed input) but it will get the job done. A little better,

if (isset($_GET['ID']) and is_numeric($_GET['ID']) and ($_GET['ID'] > 0)) {
$ID = $_GET['ID'];
}
else { die("Invalid value for record id"); }

lwaters

4:50 pm on Oct 6, 2011 (gmt 0)

10+ Year Member



Here is my code. Still not working:

<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}


$valuesSets = "";
$addit = $_POST['addit'];
for ($i=0; $i < count($addit); $i++) {
$valuesSets.="(".GetSQLValueString($UID[$i], "int").",".GetSQLValueString($ID[$i], "int")."),";
$valuesSets=substr($valuesSets,0,-1); // to remove last comma
$insertSQL = sprintf("INSERT INTO stu_schol (`UID`, `ID`) VALUES %s", $valuesSets);


mysql_select_db($database_connect, $connect);
if ($valuesSets <> "") {
// echo $insertSQL;
$Result1 = mysql_query($insertSQL, $connect) or die(mysql_error());

$insertGoTo = "main_page.php?UID=" . $_GET['UID'] . "";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}}

$colname_special_attach = "-1";
if (isset($_GET['UID'])) {
$colname_special_attach = $_GET['UID'];
}
mysql_select_db($database_connect, $connect);
$query_special_attach = "SELECT * FROM scholarships WHERE spec_attach='Y' ORDER BY scholarships.NAME ASC";
$special_attach = mysql_query($query_special_attach, $connect) or die(mysql_error());
$row_special_attach = mysql_fetch_assoc($special_attach);
$totalRows_special_attach = mysql_num_rows($special_attach);

mysql_select_db($database_connect, $connect);
$query_your_scholarships = "SELECT * FROM scholarships WHERE ACTIVE = 'Y' ORDER BY NAME ASC";
$your_scholarships = mysql_query($query_your_scholarships, $connect) or die(mysql_error());
$row_your_scholarships = mysql_fetch_assoc($your_scholarships);
$totalRows_your_scholarships = mysql_num_rows($your_scholarships);
?>


FORM:

<form method="POST" action="<?php echo $editFormAction; ?>" name="standardapps">
<input name="addit" type="checkbox" id="addit" value="Y" />
</label>
<label for="textfield"></label>
<label>Add</label>
<input name="UID[]" type="hidden" id="UID[]" value="<?php echo $_GET['UID']; ?>" />
<input name="ID[]" type="hidden" id="ID[]" value="<?php echo $row_your_scholarships['ID']; ?>" />
<input type="submit" class="btn" name="Continue &gt;&gt;2" id="Continue &gt;&gt;2" value="Update My Selections" /></td>
</tr>
<input type="hidden" name="MM_insert" value="standardapps" />
</form>

lwaters

5:03 pm on Oct 6, 2011 (gmt 0)

10+ Year Member



Is there a better way to do this? I have the database with ID, UID. The ID is the unique number for the scholarship and the UID is the student ID. I have a listing of all the scholarships that they can select. Next to each one is the checkbox and the bottom is a submit button.

penders

10:12 am on Oct 7, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



$valuesSets.="(".GetSQLValueString($UID[$i], "int").",".GetSQLValueString($ID[$i], "int")."),"; 
:
:
$insertGoTo = "main_page.php?UID=" . $_GET['UID'] . "";


You are trying to use variables before they are defined (the first 2 instances). This might work if register_globals is set, but it probably isn't (and shouldn't).

You have defined UID and ID as arrays in your form and you attempt to loop through the array in the first part of your code (submitted data). But elsewhere in your code you assume it is a string and simply
echo $_GET['UID']
. If this is an array then it will simply output the word "Array". Should they be arrays or strings? If they occur only once in your form, as hidden fields (not checkboxes) then they should probably be strings.

lwaters

12:14 pm on Oct 7, 2011 (gmt 0)

10+ Year Member



The UID is only once in the form and the ID is multiple times in the form. The checkbox doesn't correspond with any field in the database. It is just used as a Y or N. Should I have the checkbox equal to the value of ID? All of the fields are hidden. As you can see I am a PHP newbie :)

rocknbil

3:28 pm on Oct 7, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You are trying to use variables before they are defined


Agreed, which is why I asked. :-) You need to set $UID and $ID somewhere near the beginning of the execution - which, by the looks of it, comes after the function.

Second, if this is the form that posts the data,

<form method="POST"

Your values will be in $_POST, not $_GET.


As for the arrays and scalars, to put it another way, you are defining it as an array here

input name="UID[]"

Then attempting to access a scalar here

$_GET['UID'] (and other places, for ID)

An array is a list of members, a scalar is a flat single value.

One or the other requires modification. You can do this,

input name="UID"
...
$_GET['UID']

or this

input name="UID[]"
...
$_GET['UID'][0]

If there's only one value, the first member of an array will be at index 0 (zero). If you always intend to use only a single checkbox, you can just use the first case. If there's any chance it will have multiple occurrences (like, a display of several students,) use an array and dereference it properly.