Jump to content

Help with IF statement


andrew_ww

Recommended Posts

I have to do something like the following.

If tickbox1 is checked then run this INSERT statement (that'll be a specific row added), then if tickbox2 is checked as well I would like to run two INSERT statements.

Essentially I have 6 tick boxes, if all 6 are ticked then when the user clicks submits 6 new rows will have to be added.  Each row will contain a different piece of information.

Any ideas how I could accomplish this ?
Link to comment
Share on other sites

when a form is submitted, the values are stored in $_POST. If checkbox1 is named "foo" then $_POST['foo'] will contain the value of the checkbox when checked. So make the value="1" in your HTML. In your processing page, check the six boxes and see which are checked. Then run the SQL accordingly.
Link to comment
Share on other sites


I have produced the following code, but I'm getting a query empty error, does anything look wrong with this ?

[code]<?php require_once('Connections/MySQL_tick.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  $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']);
}

if ($_POST['checkbox']) {
      $insertSQL = execute_query("INSERT INTO tbl_table1 (review_type) VALUES ('tests')");
}
if ($_POST['checkbox2']) {
      $insertSQL = execute_query("INSERT INTO tbl_table1 (review_type) VALUES ('ispsdjfsdf')");
}


  mysql_select_db($database_MySQL_tick, $MySQL_tick);
  $Result1 = mysql_query($insertSQL, $MySQL_tick) or die(mysql_error());

mysql_select_db($database_MySQL_tick, $MySQL_tick);
$query_rs_insert = "SELECT * FROM tbl_table1";
$rs_insert = mysql_query($query_rs_insert, $MySQL_tick) or die(mysql_error());
$row_rs_insert = mysql_fetch_assoc($rs_insert);
$totalRows_rs_insert = mysql_num_rows($rs_insert);
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="POST" action="<?php echo $editFormAction; ?>">
  <p>
    <input type="checkbox" name="checkbox" value="checkbox" />
  Review 01 </p>
  <p>
  Review 02 </p>

    <input type="submit" name="Submit" value="Submit" />
    <input type="hidden" name="MM_insert" value="form1">

</form>
<input type="checkbox" name="checkbox2" value="checkbox" />
</body>
</html>
<?php
mysql_free_result($rs_insert);
?>[/code]
Link to comment
Share on other sites

Change your $insertSQL to just the SQL, not execute_query(). Just the STRING.
if ($_POST['checkbox']) {
      $insertSQL = "INSERT INTO tbl_table1 (review_type) VALUES ('tests')";
}
if ($_POST['checkbox2']) {
      $insertSQL = "INSERT INTO tbl_table1 (review_type) VALUES ('ispsdjfsdf')";
}

And to ensure no errors, surround the call of this in a check.
if($insertSQL){
  $Result1 = mysql_query($insertSQL, $MySQL_tick) or die(mysql_error());
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.