Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone,

I want to write a query to insert into carsafety(table#2) where carID=CarID in the cars table and cars.studentID= the studentID selected in the page. ($CustomerID2Update)

i tested this query in mySQL and it works fine, but it doesn't update from my php page.

Thanks for your help.

table cars (carID , studentID, ...)
table carsafty(carID, safetyID)



PHP
<?php
	$a = session_id();
	if(empty($a)) session_start();

	$_SESSION['CustomerID2Update'] = 0 ;

	if ($_SESSION['CustomerID']==0) {			// Not logged in
		$_SESSION['Message'] = "Insert Preferences: Permission Denied";
		header("Location: index.php") ;
	} else {
		include "MySQLConnector.txt";
	}

// 	Get the data from the form:
	$CustomerID2Update = $_REQUEST['CustomerID2Update'];	// Hidden Field


	$query = "DELETE FROM carsafety WHERE cars.CarID = carsafety.CarID AND cars.CustomerID = " . $CustomerID2Update ;

if(!empty($_REQUEST['chkSaf1'])) {
		$SafGroup1 = trim($_REQUEST['chkSaf1']);	// Constituency Group 1
			$query = "INSERT INTO carsafety CarID, SafetyID SELECT CarID, '" . $SafGroup1 . "'
		    from cars WHERE cars.CustomerID= " . $CustomerID2Update. " ";
       		mysqli_query($dbc,$query);
	}
	if(!empty($_REQUEST['chkSaf2'])) {
		$SafGroup2 = trim($_REQUEST['chkSaf2']);	// Constituency Group 2
		$query = "INSERT INTO carsafety CarID, SafetyID SELECT CarID, '" . $SafGroup2 . "'
		    from cars WHERE cars.CustomerID= " . $CustomerID2Update. " ";
       		mysqli_query($dbc,$query);
	}
	if(!empty($_REQUEST['chkSaf3'])) {
		$SafGroup3 = trim($_REQUEST['chkSaf3']);	// Constituency Group 3
		$query = "INSERT INTO carsafety CarID, SafetyID SELECT CarID, '" . $SafGroup3 . "'
		    from cars WHERE cars.CustomerID= " . $CustomerID2Update. " ";
       		mysqli_query($dbc,$query);
	}
	if(!empty($_REQUEST['chkSaf4'])) {
		$SafGroup4 = trim($_REQUEST['chkSaf4']);	// Constituency Group 4
$query = "INSERT INTO carsafety CarID, SafetyID SELECT CarID, '" . $SafGroup4 . "'
		    from cars WHERE cars.CustomerID= " . $CustomerID2Update. " ";
       		mysqli_query($dbc,$query);
	}
	if(!empty($_REQUEST['chkSaf5'])) {
		$SafGroup5 = trim($_REQUEST['chkSaf5']);	// Constituency Group 5
		$query = "INSERT INTO carsafety CarID, SafetyID SELECT CarID, '" . $SafGroup5 . "'
		    from cars WHERE cars.CustomerID= " . $CustomerID2Update. " ";
       		mysqli_query($dbc,$query);
	}
	if(!empty($_REQUEST['chkSaf6'])) {
		$SafGroup6 = trim($_REQUEST['chkSaf6']);	// Constituency Group 6
			$query = "INSERT INTO carsafety CarID, SafetyID SELECT CarID, '" . $SafGroup6 . "'
		    from cars WHERE cars.CustomerID= " . $CustomerID2Update. " ";
       		mysqli_query($dbc,$query);
	}
	if(!empty($_REQUEST['chkSaf7'])) {
		$SafGroup7 = trim($_REQUEST['chkSaf7']);	// Constituency Group 7
		$query = "INSERT INTO carsafety CarID, SafetyID SELECT CarID, '" . $SafGroup7 . "'
		    from cars WHERE cars.CustomerID= " . $CustomerID2Update. " ";
       		mysqli_query($dbc,$query);
	}
	if(!empty($_REQUEST['chkSaf8'])) {
		$SafGroup8 = trim($_REQUEST['chkSaf8']);	// Constituency Group 7
			$query = "INSERT INTO carsafety CarID, SafetyID SELECT CarID, '" . $SafGroup8 . "'
		    from cars WHERE cars.CustomerID= " . $CustomerID2Update. " ";
       		mysqli_query($dbc,$query);
	}
Posted
Updated 22-Mar-12 1:44am
v3
Comments
Herman<T>.Instance 22-Mar-12 4:22am    
if you read this[^] page I miss the mysql_connect and mysql_close in your source. Or do we miss some of your code in your example?
Mike988 22-Mar-12 7:32am    
i put the rest of my code
Herman<T>.Instance 22-Mar-12 7:48am    
I still miss the connection to the db in your source. See example 1 <a href="http://www.php.net/manual/en/mysqli.query.php" target="_blank">here
Mike988 22-Mar-12 7:58am    
this is the connection . MySQLConnector.txt
//I have a few radio buttons to update before that code and they works fine.

if ($_SESSION['CustomerID']==0) { // Not logged in
$_SESSION['Message'] = "Insert Preferences: Permission Denied";
header("Location: index.php") ;
} else {
include "MySQLConnector.txt";
}
Herman<T>.Instance 22-Mar-12 8:30am    
you only include a file you have no call to the file for opening and closing the connection.

1 solution

the problem was on the delete query . $query = "DELETE FROM carsafety WHERE EXISTS( select cars.CarID FROM cars WHERE cars.CustomerID = " . $CustomerID2Update. " AND cars.CarID = carsafety.CarID )";
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900