Jump to content

Need help on Insert data to phpMyAdmin


aoiregion

Recommended Posts

Hi guys, I would like to seek help on inserting data whenever the switch is on or off to my sensor mySQL database in phpMyAdmin from my control.php. I'm using Raspberry PI as my hardware and follow a few tutorials to create my own Web Control Interface, it works perfectly without insert method. After I implemented insert method to my control.php and execute it, it cannot works and cannot store.
 
This is my control.php:
<?php
session_start();

$MySQLUsername = "gpio";
$MYSQLUsersensor = "sensor";
$MySQLPassword = "kang";


$MySQLHost = "localhost";
$MySQLDB = "gpio";
$MYSQLDBsensor = "sensor";

If (($MySQLUsername == "USERNAME HERE") || ($MySQLPassword == "PASSWORD HERE")){
	print 'ERROR - Please set up the script first';
	exit();
}

$dbConnection = mysql_connect($MySQLHost, $MySQLUsername, $MySQLPassword);
mysql_select_db($MySQLDB, $dbConnection);
If (isset($_POST['action'])){
	If ($_POST['action'] == "setPassword"){
		$password1 = $_POST['password1'];
		$password2 = $_POST['password2'];
		If ($password1 != $password2){
			header('Location: control.php');
		}
		$password = mysql_real_escape_string($_POST['password1']);
		If (strlen($password) > 28){
			mysql_close();
			header('location: control.php');
		}
		$resetQuery = "SELECT username, salt FROM users WHERE username = 'admin';";
		$resetResult = mysql_query($resetQuery);
		If (mysql_num_rows($resetResult) < 1){
			mysql_close();
			header('location: control.php');
		}
		$resetData = mysql_fetch_array($resetResult, MYSQL_ASSOC);
		$resetHash = hash('sha256', $salt . hash('sha256', $password));
		$hash = hash('sha256', $password);
		function createSalt(){
			$string = md5(uniqid(rand(), true));
			return substr($string, 0, ;
		}
		$salt = createSalt();
		$hash = hash('sha256', $salt . $hash);
		mysql_query("UPDATE users SET salt='$salt' WHERE username='admin'");
		mysql_query("UPDATE users SET password='$hash' WHERE username='admin'");
		mysql_close();
		header('location: control.php');
	}
}
If ((isset($_POST['username'])) && (isset($_POST['password']))){
	$username = mysql_real_escape_string($_POST['username']);
	$password = mysql_real_escape_string($_POST['password']);
	$loginQuery = "SELECT UserID, password, salt FROM users WHERE username = '$username';";
	$loginResult = mysql_query($loginQuery);
	If (mysql_num_rows($loginResult) < 1){
		mysql_close();
		header('location: control.php?error=incorrectLogin');
	}
	$loginData = mysql_fetch_array($loginResult, MYSQL_ASSOC);
	$loginHash = hash('sha256', $loginData['salt'] . hash('sha256', $password));
	If ($loginHash != $loginData['password']){
		mysql_close();
		header('location: control.php?error=incorrectLogin');
	} else {
		session_regenerate_id();
		$_SESSION['username'] = "admin";
		$_SESSION['userID'] = "1";
		mysql_close();
		header('location: control.php');
	}
}
If ((!isset($_SESSION['username'])) || (!isset($_SESSION['userID']))){
	print '
	<html>
	<head>
	<title>GPIO Control - Login</title>
	</head>
	<body>
	<table border="0" align="center">
	<form name="login" action="control.php" method="post">
	<tr>
	<td>Username: </td><td><input type="text" name="username"></td>
	</tr>
	<tr>
	<td>Password: </td><td><input type="password" name="password"></td>
	</tr>
	<tr>
	<td colspan="2" align="center"><input type="submit" value="Log In"></td>
	</tr>
	</form>
	</table>
	</body>
	</html>
	';
	die();
}
If (isset($_GET['action'])){
	If ($_GET['action'] == "logout"){
		$_SESSION = array();
		session_destroy();
		header('Location: control.php');
	} else If ($_GET['action'] == "setPassword"){
		print '
		<form name="changePassword" action="control.php" method="post">
		<input type="hidden" name="action" value="setPassword">
		<p>Enter New Password: <input type="password" name="password1">  Confirm: <input type="password" name="password2"><input type="submit" value="submit"></p>
		</form>
		';
	} else {
		$now = new DateTime();
		$action = $_GET['action'];
		$pin = mysql_real_escape_string($_GET['pin']);
		if ($action == "turnOn"){
			$setting = "1";
			mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='4';");
			$dbConnectionSensor = mysql_connect($MySQLHost, $MySQLUserSensor, $MySQLPassword);
			mysql_select_db($MySQLDBSensor, $dbConnectionSensor);
			mysql_query("INSERT INTO sensor('1', 'Red LED', $now, '')");
			mysql_close();
			mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='17';");
			$dbConnectionSensor = mysql_connect($MySQLHost, $MySQLUserSensor, $MySQLPassword);
			mysql_select_db($MySQLDBSensor, $dbConnectionSensor);
			mysql_query("INSERT INTO sensor('2', 'Blue LED', $now, '')");
			mysql_close();
			mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='18';");
			$dbConnectionSensor = mysql_connect($MySQLHost, $MySQLUserSensor, $MySQLPassword);
			mysql_select_db($MySQLDBSensor, $dbConnectionSensor);
			mysql_query("INSERT INTO sensor('3', 'Green LED', $now, '')");
			mysql_close();
			mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='21';");
			$dbConnectionSensor = mysql_connect($MySQLHost, $MySQLUserSensor, $MySQLPassword);
			mysql_select_db($MySQLDBSensor, $dbConnectionSensor);
			mysql_query("INSERT INTO sensor('4', 'LED', $now, '')");
			mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='22';");
			$dbConnectionSensor = mysql_connect($MySQLHost, $MySQLUserSensor, $MySQLPassword);
			mysql_select_db($MySQLDBSensor, $dbConnectionSensor);
			mysql_query("INSERT INTO sensor('5', 'LED', $now, '')");
			mysql_close();
			mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='23';");
			$dbConnectionSensor = mysql_connect($MySQLHost, $MySQLUserSensor, $MySQLPassword);
			mysql_select_db($MySQLDBSensor, $dbConnectionSensor);
			mysql_query("INSERT INTO sensor('6', 'LED', $now, '')");
			mysql_close();
			mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='24';");
			$dbConnectionSensor = mysql_connect($MySQLHost, $MySQLUserSensor, $MySQLPassword);
			mysql_select_db($MySQLDBSensor, $dbConnectionSensor);
			mysql_query("INSERT INTO sensor('7', 'LED', $now, '')");
			mysql_close();
			mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='25';");
			$dbConnectionSensor = mysql_connect($MySQLHost, $MySQLUserSensor, $MySQLPassword);
			mysql_select_db($MySQLDBSensor, $dbConnectionSensor);
			mysql_query("INSERT INTO sensor('8', 'LED', $now, '')");
			mysql_close();
			header('Location: control.php');
		} else If ($action == "turnOff"){
			$setting = "0";
			mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='4';");
			$dbConnectionSensor = mysql_connect($MySQLHost, $MySQLUserSensor, $MySQLPassword);
			mysql_select_db($MySQLDBSensor, $dbConnectionSensor);
			mysql_query("INSERT INTO sensor('1', 'Red LED', '', $now)");
			mysql_close();
			mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='17';");
			$dbConnectionSensor = mysql_connect($MySQLHost, $MySQLUserSensor, $MySQLPassword);
			mysql_select_db($MySQLDBSensor, $dbConnectionSensor);
			mysql_query("INSERT INTO sensor('2', 'Blue LED', '', $now)");
			mysql_close();
			mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='18';");
			$dbConnectionSensor = mysql_connect($MySQLHost, $MySQLUserSensor, $MySQLPassword);
			mysql_select_db($MySQLDBSensor, $dbConnectionSensor);
			mysql_query("INSERT INTO sensor('3', 'Green LED', '', $now)");
			mysql_close();
			mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='21';");
			$dbConnectionSensor = mysql_connect($MySQLHost, $MySQLUserSensor, $MySQLPassword);
			mysql_select_db($MySQLDBSensor, $dbConnectionSensor);
			mysql_query("INSERT INTO sensor('4', 'LED', '', $now)");
			mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='22';");
			$dbConnectionSensor = mysql_connect($MySQLHost, $MySQLUserSensor, $MySQLPassword);
			mysql_select_db($MySQLDBSensor, $dbConnectionSensor);
			mysql_query("INSERT INTO sensor('5', 'LED', '', $now)");
			mysql_close();
			mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='23';");
			$dbConnectionSensor = mysql_connect($MySQLHost, $MySQLUserSensor, $MySQLPassword);
			mysql_select_db($MySQLDBSensor, $dbConnectionSensor);
			mysql_query("INSERT INTO sensor('6', 'LED', '', $now)");
			mysql_close();
			mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='24';");
			$dbConnectionSensor = mysql_connect($MySQLHost, $MySQLUserSensor, $MySQLPassword);
			mysql_select_db($MySQLDBSensor, $dbConnectionSensor);
			mysql_query("INSERT INTO sensor('7', 'LED', '', $now)");
			mysql_close();
			mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='25';");
			$dbConnectionSensor = mysql_connect($MySQLHost, $MySQLUserSensor, $MySQLPassword);
			mysql_select_db($MySQLDBSensor, $dbConnectionSensor);
			mysql_query("INSERT INTO sensor('8', 'LED', '', $now)");
			mysql_close();
			header('Location: control.php');
		} else IF ($action =="edit"){
			$pin = mysql_real_escape_string($_GET['pin']);
			$query = mysql_query("SELECT pinDescription FROM pinDescription WHERE pinNumber='$pin';");
			$descRow = mysql_fetch_assoc($query);
			$description = $descRow['pinDescription'];
			print '
			<html><head><title>Update Pin ' . $pin . '</title></head><body>
			<table border="0">
			<form name="edit" action="control.php" method="get">
			<input type="hidden" name="action" value="update">
			<input type="hidden" name="pin" value="' . $pin . '">
			<tr>
			<td><p>Description: </p></td><td><input type="text" name="description" value="' . $description . '"></td><td><input type="submit" value="Confirm"></td>
			</tr>
			</form>
			</table>
			</body></html>
			';
			mysql_close();
		} else IF ($action =="update"){
			$pin = mysql_real_escape_string($_GET['pin']);
			$description = mysql_real_escape_string($_GET['description']);
			mysql_query("UPDATE pinDescription SET pinDescription='$description' WHERE pinNumber='$pin';");
			header('Location: control.php');
		} else {
			header('Location: control.php');
		}
	}
} else {
	print '
		<html>
		<head>
		<title>GPIO Control</title>
		</head>
		<font face="verdana">
		<p><h1>Sensors</h1></p>
		';
		$query = mysql_query("SELECT pinNumber, pinStatus FROM pinStatus;");
		$query2 = mysql_query("SELECT pinNumber, pinDescription FROM pinDescription;");
		$totalGPIOCount = mysql_num_rows($query);
		$currentGPIOCount = 0;
		print '<table name="GPIO" border="1" cellpadding="5">';
		print '<tr><th>GPIO #</th><th>GPIO Description</th><th>Status</th><th>Action</th><th>Edit</th></tr>';
		while ($currentGPIOCount < $totalGPIOCount){
			$pinRow = mysql_fetch_assoc($query);
			$descRow = mysql_fetch_assoc($query2);
			$pinNumber = $pinRow['pinNumber'];
			$pinStatus = $pinRow['pinStatus'];
			$pinDescription = $descRow['pinDescription'];
			If ($pinStatus == "0"){
				$buttonValue = "Turn On";
				$action = "turnOn";
				$image = "off.jpg";
			} else {
				$buttonValue = "Turn Off";
				$action = "turnOff";
				$image = "on.jpg";
			}
			print '<tr>';
			print '<td align="center">' . $pinNumber . '</td><td>' . $pinDescription . '</td><td align="center"><img src="' . $image . '" width="50"></td><td align="center" valign="middle"><form name="pin' . $pinNumber . 'edit" action="control.php" method="get"><input type="hidden" name="action" value="' . $action . '"><input type="hidden" name="pin" value="' . $pinNumber . '"><input type="submit" value="' . $buttonValue . '"></form></td><td><form name="pin' . $pinNumber . '" action="control.php" method="get"><input type="hidden" name="action" value="edit"><input type="hidden" name="pin" value="' . $pinNumber . '"><input type="submit" value="Edit"></form></td>';
			print '</tr>';
			$currentGPIOCount ++;
		}
		print '</table>';
		mysql_close();
	print '
	<br><br>
	<a href="control.php?action=logout">Log Out</a>
	</font>
	</html>
	';
}
?>

This is my pinDirection SQL database:

DROP TABLE IF EXISTS `pinDirection`;
CREATE TABLE IF NOT EXISTS `pinDirection` (
  `pinID` int(11) NOT NULL AUTO_INCREMENT,
  `pinNumber` varchar(2) COLLATE utf8_unicode_ci NOT NULL,
  `pinDirection` varchar(3) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`pinID`),
  UNIQUE KEY `pinNumber` (`pinNumber`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=9 ;

This is my pinStatus SQL database:

DROP TABLE IF EXISTS `pinStatus`;
CREATE TABLE IF NOT EXISTS `pinStatus` (
  `pinID` int(11) NOT NULL AUTO_INCREMENT,
  `pinNumber` varchar(2) COLLATE utf8_unicode_ci NOT NULL,
  `pinStatus` varchar(1) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`pinID`),
  UNIQUE KEY `pinNumber` (`pinNumber`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=9 ;

This is my sensor SQL database:

CREATE TABLE IF NOT EXISTS `sensor` (
  `id` int(11) NOT NULL,
  `sensor` text NOT NULL,
  `switchOnLog` datetime NOT NULL,
  `switchOffLog` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Hope you guys could help me out with it as soon as possible, thanks in advance, guys

Link to comment
Share on other sites

 

 

 After I implemented insert method to my control.php and execute it, it cannot works and cannot store.

What exactly? You need to explain what you are wanting to inserting, when and where in the code you need this to take place.

 

Before you do this though you code needs optimizing.

 

First there is no need for you to have separate MySQL users for different tables in your database. It is ok to have different MySQL users if your script deals with multiple mysql servers but not when the two users are for the same server and database. This just adds unneeded complexity. So refactor your code so you only have to deal with one mysql user instance.

 

Next there is no need to open/close a connection to the mysql database before/after every query. You should open the connection once at the very beginning of the script and leave the connection open for remainder of your script. PHP will automatically close the connection to mysql after script execution. Connecting/disconnecting multiple times to the mysql server just wastes server resources.

 

What is going on here?

...
	} else {
		$now = new DateTime();
		$action = $_GET['action'];
		$pin = mysql_real_escape_string($_GET['pin']);
		if ($action == "turnOn"){
			$setting = "1";
			mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='4';");
			$dbConnectionSensor = mysql_connect($MySQLHost, $MySQLUserSensor, $MySQLPassword);
			mysql_select_db($MySQLDBSensor, $dbConnectionSensor);
			mysql_query("INSERT INTO sensor('1', 'Red LED', $now, '')");
			mysql_close();
			mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='17';");
			$dbConnectionSensor = mysql_connect($MySQLHost, $MySQLUserSensor, $MySQLPassword);
			mysql_select_db($MySQLDBSensor, $dbConnectionSensor);
			mysql_query("INSERT INTO sensor('2', 'Blue LED', $now, '')");
			mysql_close();
			mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='18';");
			$dbConnectionSensor = mysql_connect($MySQLHost, $MySQLUserSensor, $MySQLPassword);
			mysql_select_db($MySQLDBSensor, $dbConnectionSensor);
			mysql_query("INSERT INTO sensor('3', 'Green LED', $now, '')");
			mysql_close();
			mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='21';");
			$dbConnectionSensor = mysql_connect($MySQLHost, $MySQLUserSensor, $MySQLPassword);
			mysql_select_db($MySQLDBSensor, $dbConnectionSensor);
			mysql_query("INSERT INTO sensor('4', 'LED', $now, '')");
			mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='22';");
			$dbConnectionSensor = mysql_connect($MySQLHost, $MySQLUserSensor, $MySQLPassword);
			mysql_select_db($MySQLDBSensor, $dbConnectionSensor);
			mysql_query("INSERT INTO sensor('5', 'LED', $now, '')");
			mysql_close();
			mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='23';");
			$dbConnectionSensor = mysql_connect($MySQLHost, $MySQLUserSensor, $MySQLPassword);
			mysql_select_db($MySQLDBSensor, $dbConnectionSensor);
			mysql_query("INSERT INTO sensor('6', 'LED', $now, '')");
			mysql_close();
			mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='24';");
			$dbConnectionSensor = mysql_connect($MySQLHost, $MySQLUserSensor, $MySQLPassword);
			mysql_select_db($MySQLDBSensor, $dbConnectionSensor);
			mysql_query("INSERT INTO sensor('7', 'LED', $now, '')");
			mysql_close();
			mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='25';");
			$dbConnectionSensor = mysql_connect($MySQLHost, $MySQLUserSensor, $MySQLPassword);
			mysql_select_db($MySQLDBSensor, $dbConnectionSensor);
			mysql_query("INSERT INTO sensor('8', 'LED', $now, '')");
			mysql_close();
			header('Location: control.php');
		} else If ($action == "turnOff"){
			$setting = "0";
			mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='4';");
			$dbConnectionSensor = mysql_connect($MySQLHost, $MySQLUserSensor, $MySQLPassword);
			mysql_select_db($MySQLDBSensor, $dbConnectionSensor);
			mysql_query("INSERT INTO sensor('1', 'Red LED', '', $now)");
			mysql_close();
			mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='17';");
			$dbConnectionSensor = mysql_connect($MySQLHost, $MySQLUserSensor, $MySQLPassword);
			mysql_select_db($MySQLDBSensor, $dbConnectionSensor);
			mysql_query("INSERT INTO sensor('2', 'Blue LED', '', $now)");
			mysql_close();
			mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='18';");
			$dbConnectionSensor = mysql_connect($MySQLHost, $MySQLUserSensor, $MySQLPassword);
			mysql_select_db($MySQLDBSensor, $dbConnectionSensor);
			mysql_query("INSERT INTO sensor('3', 'Green LED', '', $now)");
			mysql_close();
			mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='21';");
			$dbConnectionSensor = mysql_connect($MySQLHost, $MySQLUserSensor, $MySQLPassword);
			mysql_select_db($MySQLDBSensor, $dbConnectionSensor);
			mysql_query("INSERT INTO sensor('4', 'LED', '', $now)");
			mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='22';");
			$dbConnectionSensor = mysql_connect($MySQLHost, $MySQLUserSensor, $MySQLPassword);
			mysql_select_db($MySQLDBSensor, $dbConnectionSensor);
			mysql_query("INSERT INTO sensor('5', 'LED', '', $now)");
			mysql_close();
			mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='23';");
			$dbConnectionSensor = mysql_connect($MySQLHost, $MySQLUserSensor, $MySQLPassword);
			mysql_select_db($MySQLDBSensor, $dbConnectionSensor);
			mysql_query("INSERT INTO sensor('6', 'LED', '', $now)");
			mysql_close();
			mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='24';");
			$dbConnectionSensor = mysql_connect($MySQLHost, $MySQLUserSensor, $MySQLPassword);
			mysql_select_db($MySQLDBSensor, $dbConnectionSensor);
			mysql_query("INSERT INTO sensor('7', 'LED', '', $now)");
			mysql_close();
			mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='25';");
			$dbConnectionSensor = mysql_connect($MySQLHost, $MySQLUserSensor, $MySQLPassword);
			mysql_select_db($MySQLDBSensor, $dbConnectionSensor);
			mysql_query("INSERT INTO sensor('8', 'LED', '', $now)");
			mysql_close();
			header('Location: control.php');
		}

In this part aren't you supposed to be toggling the pinStatus (between 1 and 0) where $_GET['pin'] matches pinNumber. Ie you click the Tun On button from the Sessions table for GPIO Pin #4 the code will then set its pinSatus to 1. Clicking the Turn Off button again for this pin will set its pinStatus value to 0.

 

With your code at it is, any button you press from your Sensors table will set all the pins statuses to the same values! Not each one being updated individually.

 

To have the pins update individually you need to the change the above code to something like

...
} else {
       $pin = intval($_GET['pin']); // get the pin id

       $setting = ($_GET['action'] == "turnOn") ? 1 : 0; // decide the new pinStatus value, eg If action is 'turnOn' set pinStatus to 1 otherwise set pinStatus to 0 ('turnOff')

       mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber=$pin"); // update the pinStatus value where pinNumber matches $_GET['pin']
}

The next issue you have is ideally you'd should have your forms submit their data via POST not GET.

 

Also you should ideally use MYSQLi or PDO as the mysql_* functions are deprecated, which means they are no longer supported and could be removed from future versions of PHP. 

Link to comment
Share on other sites

 

 

 

Sorry Ch0cu3r, kinda confused now and hope you don't mind me asking alot cause started PHP last week, thanks.

So I should create a new PHP to submit data to my sensor database in phpmyadmin is it?

If really that the case, can I don't cause I want all my function of switching on and off LED plus storing data in one PHP instead of two.

Hope you can guide me along, thanks in advance.

Edited by Ch0cu3r
Link to comment
Share on other sites

This is my latest edited code where I found out that I set my POST in GET and I have amended it already but the problem is still there.

<?php
session_start();

$MySQLUsername = "MySQLUsername";
$MYSQLUsersensor = "MYSQLUsersensor";
$MySQLPassword = "MySQLPassword";


$MySQLHost = "localhost";
$MySQLDB = "gpio";
$MYSQLDBsensor = "sensor";

If (($MySQLUsername == "USERNAME HERE") || ($MySQLPassword == "PASSWORD HERE")){
	print 'ERROR - Please set up the script first';
	exit();
}

$dbConnection = mysql_connect($MySQLHost, $MySQLUsername, $MySQLPassword);
mysql_select_db($MySQLDB, $dbConnection);
If (isset($_POST['action'])){
	If ($_POST['action'] == "setPassword"){
		$password1 = $_POST['password1'];
		$password2 = $_POST['password2'];
		If ($password1 != $password2){
			header('Location: control.php');
		}
		$password = mysql_real_escape_string($_POST['password1']);
		If (strlen($password) > 28){
			mysql_close();
			header('location: control.php');
		}
		$resetQuery = "SELECT username, salt FROM users WHERE username = 'admin';";
		$resetResult = mysql_query($resetQuery);
		If (mysql_num_rows($resetResult) < 1){
			mysql_close();
			header('location: control.php');
		}
		$resetData = mysql_fetch_array($resetResult, MYSQL_ASSOC);
		$resetHash = hash('sha256', $salt . hash('sha256', $password));
		$hash = hash('sha256', $password);
		function createSalt(){
			$string = md5(uniqid(rand(), true));
			return substr($string, 0, ;
		}
		$salt = createSalt();
		$hash = hash('sha256', $salt . $hash);
		mysql_query("UPDATE users SET salt='$salt' WHERE username='admin'");
		mysql_query("UPDATE users SET password='$hash' WHERE username='admin'");
		mysql_close();
		header('location: control.php');
	}
}
If ((isset($_POST['username'])) && (isset($_POST['password']))){
	$username = mysql_real_escape_string($_POST['username']);
	$password = mysql_real_escape_string($_POST['password']);
	$loginQuery = "SELECT UserID, password, salt FROM users WHERE username = '$username';";
	$loginResult = mysql_query($loginQuery);
	If (mysql_num_rows($loginResult) < 1){
		mysql_close();
		header('location: control.php?error=incorrectLogin');
	}
	$loginData = mysql_fetch_array($loginResult, MYSQL_ASSOC);
	$loginHash = hash('sha256', $loginData['salt'] . hash('sha256', $password));
	If ($loginHash != $loginData['password']){
		mysql_close();
		header('location: control.php?error=incorrectLogin');
	} else {
		session_regenerate_id();
		$_SESSION['username'] = "admin";
		$_SESSION['userID'] = "1";
		mysql_close();
		header('location: control.php');
	}
}
If ((!isset($_SESSION['username'])) || (!isset($_SESSION['userID']))){
	print '
	<html>
	<head>
	<title>GPIO Control - Login</title>
	</head>
	<body>
	<table border="0" align="center">
	<form name="login" action="control.php" method="post">
	<tr>
	<td>Username: </td><td><input type="text" name="username"></td>
	</tr>
	<tr>
	<td>Password: </td><td><input type="password" name="password"></td>
	</tr>
	<tr>
	<td colspan="2" align="center"><input type="submit" value="Log In"></td>
	</tr>
	</form>
	</table>
	</body>
	</html>
	';
	die();
}
If (isset($_GET['action'])){
	If ($_GET['action'] == "logout"){
		$_SESSION = array();
		session_destroy();
		header('Location: control.php');
	} else If ($_GET['action'] == "setPassword"){
		print '
		<form name="changePassword" action="control.php" method="post">
		<input type="hidden" name="action" value="setPassword">
		<p>Enter New Password: <input type="password" name="password1">  Confirm: <input type="password" name="password2"><input type="submit" value="submit"></p>
		</form>
		';
	} else {
		$action = $_GET['action'];
		$pin = mysql_real_escape_string($_GET['pin']);
		if ($action == "turnOn"){
			$setting = "1";
			mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='$pin';");
			mysql_close();
			header('Location: control.php');
		} else If ($action == "turnOff"){
			$setting = "0";
			mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='$pin';");
			mysql_close();
			header('Location: control.php');
		} else IF ($action =="edit"){
			$pin = mysql_real_escape_string($_GET['pin']);
			$query = mysql_query("SELECT pinDescription FROM pinDescription WHERE pinNumber='$pin';");
			$descRow = mysql_fetch_assoc($query);
			$description = $descRow['pinDescription'];
			print '
			<html><head><title>Update Pin ' . $pin . '</title></head><body>
			<table border="0">
			<form name="edit" action="control.php" method="get">
			<input type="hidden" name="action" value="update">
			<input type="hidden" name="pin" value="' . $pin . '">
			<tr>
			<td><p>Description: </p></td><td><input type="text" name="description" value="' . $description . '"></td><td><input type="submit" value="Confirm"></td>
			</tr>
			</form>
			</table>
			</body></html>
			';
			mysql_close();
		} else IF ($action =="update"){
			$pin = mysql_real_escape_string($_GET['pin']);
			$description = mysql_real_escape_string($_GET['description']);
			mysql_query("UPDATE pinDescription SET pinDescription='$description' WHERE pinNumber='$pin';");
			header('Location: control.php');
		} else {
			header('Location: control.php');
		}
	} else {
                $now = new DateTime();
		$action = $_POST['action'];
		$pin = mysql_real_escape_string($_POST['pin']);
		$dbConnectionSensor = mysql_connect($MySQLHost, $MySQLUserSensor, $MySQLPassword);
		mysql_select_db($MySQLDBSensor, $dbConnectionSensor);
		if ($action == "turnOn" && "4"){
			$setting = "1";
			mysql_query("INSERT INTO sensor('1', 'Red LED', $now, '')");
		} else If ($action == "turnOn" && "17"){
			$setting = "1";
			mysql_query("INSERT INTO sensor('2', 'Blue LED', $now, '')");
		} else If ($action == "turnOn" && "18"){
			$setting = "1";
			mysql_query("INSERT INTO sensor('3', 'Green LED', $now, '')");
		} else If ($action == "turnOn" && "21"){
			$setting = "1";
			mysql_query("INSERT INTO sensor('4', 'LED', $now, '')");
		} else If ($action == "turnOn" && "22"){
			$setting = "1";
			mysql_query("INSERT INTO sensor('5', 'LED', $now, '')");
		} else If ($action == "turnOn" && "23"){
			$setting = "1";
			mysql_query("INSERT INTO sensor('6', 'LED', $now, '')");
		} else If ($action == "turnOn" && "24"){
			$setting = "1";
			mysql_query("INSERT INTO sensor('7', 'LED', $now, '')");
		} else If ($action == "turnOn" && "25"){
			$setting = "1";
			mysql_query("INSERT INTO sensor('8', 'LED', $now, '')");
			mysql_close();
			header('Location: control.php');
		} else If ($action == "turnOff" && "4"){
			$setting = "0";
			mysql_query("INSERT INTO sensor('1', 'Red LED', '', $now)");
		} else If ($action == "turnOff" && "17"){
			$setting = "0";
			mysql_query("INSERT INTO sensor('2', 'Blue LED', '', $now)");
		} else If ($action == "turnOff" && "18"){
			$setting = "0";
			mysql_query("INSERT INTO sensor('3', 'Green LED', '', $now)");
		} else If ($action == "turnOff" && "21"){
			$setting = "0";
			mysql_query("INSERT INTO sensor('4', 'LED', '', $now)");
		} else If ($action == "turnOff" && "22"){
			$setting = "0";
			mysql_query("INSERT INTO sensor('5', 'LED', '', $now)");
		} else If ($action == "turnOff" && "23"){
			$setting = "0";
			mysql_query("INSERT INTO sensor('6', 'LED', '', $now)");
		} else If ($action == "turnOff" && "24"){
			$setting = "0";
			mysql_query("INSERT INTO sensor('7', 'LED', '', $now)");
		} else If ($action == "turnOff" && "25"){
			$setting = "0";
			mysql_query("INSERT INTO sensor('8', 'LED', '', $now)");
			mysql_close();
			header('Location: control.php');
		}
	}
} else {
	print '
		<html>
		<head>
		<title>GPIO Control</title>
		</head>
		<font face="verdana">
		<p><h1>Sensors</h1></p>
		';
		$query = mysql_query("SELECT pinNumber, pinStatus FROM pinStatus;");
		$query2 = mysql_query("SELECT pinNumber, pinDescription FROM pinDescription;");
		$totalGPIOCount = mysql_num_rows($query);
		$currentGPIOCount = 0;
		print '<table name="GPIO" border="1" cellpadding="5">';
		print '<tr><th>GPIO #</th><th>GPIO Description</th><th>Status</th><th>Action</th><th>Edit</th></tr>';
		while ($currentGPIOCount < $totalGPIOCount){
			$pinRow = mysql_fetch_assoc($query);
			$descRow = mysql_fetch_assoc($query2);
			$pinNumber = $pinRow['pinNumber'];
			$pinStatus = $pinRow['pinStatus'];
			$pinDescription = $descRow['pinDescription'];
			If ($pinStatus == "0"){
				$buttonValue = "Turn On";
				$action = "turnOn";
				$image = "off.jpg";
			} else {
				$buttonValue = "Turn Off";
				$action = "turnOff";
				$image = "on.jpg";
			}
			print '<tr>';
			print '<td align="center">' . $pinNumber . '</td><td>' . $pinDescription . '</td><td align="center"><img src="' . $image . '" width="50"></td><td align="center" valign="middle"><form name="pin' . $pinNumber . 'edit" action="control.php" method="get"><input type="hidden" name="action" value="' . $action . '"><input type="hidden" name="pin" value="' . $pinNumber . '"><input type="submit" value="' . $buttonValue . '"></form></td><td><form name="pin' . $pinNumber . '" action="control.php" method="get"><input type="hidden" name="action" value="edit"><input type="hidden" name="pin" value="' . $pinNumber . '"><input type="submit" value="Edit"></form></td>';
			print '</tr>';
			$currentGPIOCount ++;
		}
		print '</table>';
		mysql_close();
	print '
	<br><br>
	<a href="control.php?action=logout">Log Out</a>
	</font>
	</html>
	';
}
?>
Link to comment
Share on other sites

Sorry Ch0cu3r, kinda confused now and hope you don't mind me asking alot cause started PHP last week, thanks.

 

What I was getting at was you can just change the the else/elseif block of code that I had quoted with this else block

} else {
       $pin = intval($_GET['pin']); // get the pin id

       $setting = ($_GET['action'] == "turnOn") ? 1 : 0; // decide the new pinStatus value, eg If action is 'turnOn' set pinStatus to 1 otherwise set pinStatus to 0 ('turnOff')

       mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber=$pin"); // update the pinStatus value where pinNumber matches $_GET['pin']
}

 

So I should create a new PHP to submit data to my sensor database in phpmyadmin is it?

If really that the case, can I don't cause I want all my function of switching on and off LED plus storing data in one PHP instead of two.

I never said to separate your code into separate files.

 

 

 

This is my latest edited code where I found out that I set my POST in GET and I have amended it already but the problem is still there.

I am only posting suggestions for how to improve your code.  Currently we do not know what that problem is as you have not told us. I would recommend that explain what is it is you are trying to do and then explain what the code is not doing.

Link to comment
Share on other sites

i have some additional recommendations -

 

1) you need to ALWAYS have php's error_reporting set to E_ALL and for development, set display_ errors to ON so that php will help you by reporting and displaying all the errors it detects. on a live(public) server, you would instead turn display display_errors OFF and turn log_errors ON so that any php detected errors would be logged instead.

 

2) you need to ALWAYS test if your (my)sql queries are working or not (returns a false value.)  for development, you would display any database errors, on a live server, you would log any database errors. you can use trigger_error() to do this because it uses the same error_reporting/display_errors/log_error settings that i mentioned in item #1 above.

 

3) Don't Repeat Yourself (DRY.) in programming, you should not find yourself repeating code that only varies in the value it operates on or in the output it displays. this is what variables are for, to let you use ONE instance of code with different input values or to produce different output to be displayed on a web page using the same layout. using this DRY method, you will have less code to write, test, debug, fix, and to make changes to. 

 

3.1) Ch0cu3r has already given you an example of doing this for the UPDATE query. you would use something similar for the INSERT query.

 

i notice that you have some sort of mapping between the pin number and sensor number? since this is all software, afaik the inputs are coming from a software controlled device, you might as well use logical pin numbers like 1,2,3,... if by some chance you do need to have an arbitrary mapping between the pin number and the sensor number, use a 'look up' array to do the mapping. there's no need to write out 8/16 sets of code when the only thing that changes is a value.

 

a look up array would be like - 

$map_inputs[4] = 1; // pin 4, sensor 1
$map_inputs[17] = 2; // pin 17, sensor 2
​... repeat for all sets of values

to use this, assuming $pin contains a valid (integer) pin number (see some of the code Ch0cu3r has given) - 

$sensor_number = isset($map_inputs[$pin]) ? $map_inputs[$pin] : false;

3.2) your <html> ... </html> page layout should only exist once in your code, near the bottom of the code, after a majority of the php logic (you have some missing html now because of the current repetition.) to accomplish this, you would build the specific output that's different in any section of code in php variable(s), then echo those php variables in one copy of the <html> ... </html> output. this is essentially using php as a template engine. for what you are doing, you would have a $title and a $content variable from each section of code.

 

4) your various tests of the 'action' value need to use a switch/case statement. this will clean up the code and make it more readable.

 

edit: since your INSERT queries seem to be related to your UPDATE queries, why did you make a separate conditional branch? i think if you do item #4 on my list, your code will be greatly simplified (you will be able to see the forest, instead of just the trees.)

Link to comment
Share on other sites

Thanks a lot, Ch0cu3r and mac_gyver xD
I have tried Ch0cu3r's method and it can't work, maybe I don't know how to implement it in the right way, so please be easy on me.

This is my newly edited code:

<?php
error_reporting(E_ALL);
ini_set('display_errors','On');

session_start();

$MySQLUsername = "gpio";
$MySQLPassword = "kang";

$MySQLHost = "localhost";
$MySQLDB = "gpio";

If (($MySQLUsername == "USERNAME HERE") || ($MySQLPassword == "PASSWORD HERE")){
	print 'ERROR - Please set up the script first';
	exit();
}

$dbConnection = mysql_connect($MySQLHost, $MySQLUsername, $MySQLPassword);
mysql_select_db($MySQLDB, $dbConnection);
If (isset($_POST['action'])){
	If ($_POST['action'] == "setPassword"){
		$password1 = $_POST['password1'];
		$password2 = $_POST['password2'];
		If ($password1 != $password2){
			header('Location: control.php');
		}
		$password = mysql_real_escape_string($_POST['password1']);
		If (strlen($password) > 28){
			mysql_close();
			header('location: control.php');
		}
		$resetQuery = "SELECT username, salt FROM users WHERE username = 'admin';";
		$resetResult = mysql_query($resetQuery);
		If (mysql_num_rows($resetResult) < 1){
			mysql_close();
			header('location: control.php');
		}
		$resetData = mysql_fetch_array($resetResult, MYSQL_ASSOC);
		$resetHash = hash('sha256', $salt . hash('sha256', $password));
		$hash = hash('sha256', $password);
		function createSalt(){
			$string = md5(uniqid(rand(), true));
			return substr($string, 0, ;
		}
		$salt = createSalt();
		$hash = hash('sha256', $salt . $hash);
		mysql_query("UPDATE users SET salt='$salt' WHERE username='admin'");
		mysql_query("UPDATE users SET password='$hash' WHERE username='admin'");
		mysql_close();
		header('location: control.php');
	}
}
If ((isset($_POST['username'])) && (isset($_POST['password']))){
	$username = mysql_real_escape_string($_POST['username']);
	$password = mysql_real_escape_string($_POST['password']);
	$loginQuery = "SELECT UserID, password, salt FROM users WHERE username = '$username';";
	$loginResult = mysql_query($loginQuery);
	If (mysql_num_rows($loginResult) < 1){
		mysql_close();
		header('location: control.php?error=incorrectLogin');
	}
	$loginData = mysql_fetch_array($loginResult, MYSQL_ASSOC);
	$loginHash = hash('sha256', $loginData['salt'] . hash('sha256', $password));
	If ($loginHash != $loginData['password']){
		mysql_close();
		header('location: control.php?error=incorrectLogin');
	} else {
		session_regenerate_id();
		$_SESSION['username'] = "admin";
		$_SESSION['userID'] = "1";
		mysql_close();
		header('location: control.php');
	}
}
If ((!isset($_SESSION['username'])) || (!isset($_SESSION['userID']))){
	print '
	<html>
	<head>
	<title>GPIO Control - Login</title>
	</head>
	<body>
	<table border="0" align="center">
	<form name="login" action="control.php" method="post">
	<tr>
	<td>Username: </td><td><input type="text" name="username"></td>
	</tr>
	<tr>
	<td>Password: </td><td><input type="password" name="password"></td>
	</tr>
	<tr>
	<td colspan="2" align="center"><input type="submit" value="Log In"></td>
	</tr>
	</form>
	</table>
	</body>
	</html>
	';
	die();
}
If (isset($_GET['action'])){
	If ($_GET['action'] == "logout"){
		$_SESSION = array();
		session_destroy();
		header('Location: control.php');
	} else If ($_GET['action'] == "setPassword"){
		print '
		<form name="changePassword" action="control.php" method="post">
		<input type="hidden" name="action" value="setPassword">
		<p>Enter New Password: <input type="password" name="password1">  Confirm: <input type="password" name="password2"><input type="submit" value="submit"></p>
		</form>
		';
	} else {
		$action = $_GET['action'];
		$storing = $_POST['storing'];
		$pin = mysql_real_escape_string($_GET['pin']);
		if ($action == "turnOn"){
			$setting = "1";
			mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='$pin';");
			mysql_close();
			header('Location: control.php');
		} else If ($action == "turnOff"){
			$setting = "0";
			mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='$pin';");
			mysql_close();
			header('Location: control.php');
		} else If ($storing == "turnOn"){
			mysql_query("INSERT INTO 'gpio'.'sensor' ('id', 'sensorId', 'sensor', 'switchOnLog', 'switchOffLog') VALUES (NULL, '1', 'Red LED', NOW(), '') FROM pinStatus WHERE pinNumber='4';");
			mysql_query("INSERT INTO 'gpio'.'sensor' ('id', 'sensorId', 'sensor', 'switchOnLog', 'switchOffLog') VALUES (NULL, '2', 'Blue LED', NOW(), '') FROM pinStatus WHERE pinNumber='17';");
			mysql_query("INSERT INTO 'gpio'.'sensor' ('id', 'sensorId', 'sensor', 'switchOnLog', 'switchOffLog') VALUES (NULL, '3', 'LED', NOW(), '') FROM pinStatus WHERE pinNumber='18';");
			mysql_query("INSERT INTO 'gpio'.'sensor' ('id', 'sensorId', 'sensor', 'switchOnLog', 'switchOffLog') VALUES (NULL, '4', 'LED', NOW(), '') FROM pinStatus WHERE pinNumber='21';");
			mysql_query("INSERT INTO 'gpio'.'sensor' ('id', 'sensorId', 'sensor', 'switchOnLog', 'switchOffLog') VALUES (NULL, '5', 'LED', NOW(), '') FROM pinStatus WHERE pinNumber='22';");
			mysql_query("INSERT INTO 'gpio'.'sensor' ('id', 'sensorId', 'sensor', 'switchOnLog', 'switchOffLog') VALUES (NULL, '6', 'Green LED', NOW(), '') FROM pinStatus WHERE pinNumber='23';");
			mysql_query("INSERT INTO 'gpio'.'sensor' ('id', 'sensorId', 'sensor', 'switchOnLog', 'switchOffLog') VALUES (NULL, '7', 'LED', NOW(), '') FROM pinStatus WHERE pinNumber='24';");
			mysql_query("INSERT INTO 'gpio'.'sensor' ('id', 'sensorId', 'sensor', 'switchOnLog', 'switchOffLog') VALUES (NULL, '8', 'LED', NOW(), '') FROM pinStatus WHERE pinNumber='25';");
			mysql_close();
			header('Location: control.php');
		} else If ($storing == "turnOff"){
			mysql_query("INSERT INTO 'gpio'.'sensor' ('id', 'sensorId', 'sensor', 'switchOnLog', 'switchOffLog') VALUES (NULL, '1', 'Red LED', '', NOW()) FROM pinStatus WHERE pinNumber='4';");
			mysql_query("INSERT INTO 'gpio'.'sensor' ('id', 'sensorId', 'sensor', 'switchOnLog', 'switchOffLog') VALUES (NULL, '2', 'Blue LED', '', NOW()) FROM pinStatus WHERE pinNumber='17';");
			mysql_query("INSERT INTO 'gpio'.'sensor' ('id', 'sensorId', 'sensor', 'switchOnLog', 'switchOffLog') VALUES (NULL, '3', 'LED', '', NOW()) FROM pinStatus WHERE pinNumber='18';");
			mysql_query("INSERT INTO 'gpio'.'sensor' ('id', 'sensorId', 'sensor', 'switchOnLog', 'switchOffLog') VALUES (NULL, '4', 'LED', '', NOW()) FROM pinStatus WHERE pinNumber='21';");
			mysql_query("INSERT INTO 'gpio'.'sensor' ('id', 'sensorId', 'sensor', 'switchOnLog', 'switchOffLog') VALUES (NULL, '5', 'LED', '', NOW()) FROM pinStatus WHERE pinNumber='22';");
			mysql_query("INSERT INTO 'gpio'.'sensor' ('id', 'sensorId', 'sensor', 'switchOnLog', 'switchOffLog') VALUES (NULL, '6', 'Green LED', '', NOW()) FROM pinStatus WHERE pinNumber='23';");
			mysql_query("INSERT INTO 'gpio'.'sensor' ('id', 'sensorId', 'sensor', 'switchOnLog', 'switchOffLog') VALUES (NULL, '7', 'LED', '', NOW()) FROM pinStatus WHERE pinNumber='24';");
			mysql_query("INSERT INTO 'gpio'.'sensor' ('id', 'sensorId', 'sensor', 'switchOnLog', 'switchOffLog') VALUES (NULL, '8', 'LED', '', NOW()) FROM pinStatus WHERE pinNumber='25';");
			mysql_close();
			header('Location: control.php');
		} else IF ($action =="edit"){
			$pin = mysql_real_escape_string($_GET['pin']);
			$query = mysql_query("SELECT pinDescription FROM pinDescription WHERE pinNumber='$pin';");
			$descRow = mysql_fetch_assoc($query);
			$description = $descRow['pinDescription'];
			print '
			<html><head><title>Update Pin ' . $pin . '</title></head><body>
			<table border="0">
			<form name="edit" action="control.php" method="get">
			<input type="hidden" name="action" value="update">
			<input type="hidden" name="pin" value="' . $pin . '">
			<tr>
			<td><p>Description: </p></td><td><input type="text" name="description" value="' . $description . '"></td><td><input type="submit" value="Confirm"></td>
			</tr>
			</form>
			</table>
			</body></html>
			';
			mysql_close();
		} else IF ($action =="update"){
			$pin = mysql_real_escape_string($_GET['pin']);
			$description = mysql_real_escape_string($_GET['description']);
			mysql_query("UPDATE pinDescription SET pinDescription='$description' WHERE pinNumber='$pin';");
			header('Location: control.php');
		} else {
			header('Location: control.php');
		}
	}
} else {
	print '
		<html>
		<head>
		<title>GPIO Control</title>
		</head>
		<font face="verdana">
		<p><h1>Sensors</h1></p>
		';
		$query = mysql_query("SELECT pinNumber, pinStatus FROM pinStatus;");
		$query2 = mysql_query("SELECT pinNumber, pinDescription FROM pinDescription;");
		$totalGPIOCount = mysql_num_rows($query);
		$currentGPIOCount = 0;
		print '<table name="GPIO" border="1" cellpadding="5">';
		print '<tr><th>GPIO #</th><th>GPIO Description</th><th>Status</th><th>Action</th><th>Edit</th></tr>';
		while ($currentGPIOCount < $totalGPIOCount){
			$pinRow = mysql_fetch_assoc($query);
			$descRow = mysql_fetch_assoc($query2);
			$pinNumber = $pinRow['pinNumber'];
			$pinStatus = $pinRow['pinStatus'];
			$pinDescription = $descRow['pinDescription'];
			If ($pinStatus == "0"){
				$buttonValue = "Turn On";
				$action = "turnOn";
				$image = "off.jpg";
			} else {
				$buttonValue = "Turn Off";
				$action = "turnOff";
				$image = "on.jpg";
			}
			print '<tr>';
			print '<td align="center">' . $pinNumber . '</td><td>' . $pinDescription . '</td><td align="center"><img src="' . $image . '" width="50"></td><td align="center" valign="middle"><form name="pin' . $pinNumber . 'edit" action="control.php" method="get"><input type="hidden" name="action" value="' . $action . '"><input type="hidden" name="pin" value="' . $pinNumber . '"><input type="submit" value="' . $buttonValue . '"></form></td><td><form name="pin' . $pinNumber . '" action="control.php" method="get"><input type="hidden" name="action" value="edit"><input type="hidden" name="pin" value="' . $pinNumber . '"><input type="submit" value="Edit"></form></td>';
			print '</tr>';
			$currentGPIOCount ++;
		}
		print '</table>';
		mysql_close();
	print '
	<br><br>
	<a href="control.php?action=logout">Log Out</a>
	</font>
	</html>
	';
}
?>

Hope you guys could help me out with it as soon as possible, thanks in advance, guys.

Link to comment
Share on other sites

 

I have tried Ch0cu3r's method and it can't work, maybe I don't know how to implement it in the right way, so please be easy on me.

This is my newly edited code:

You sure? I have tested your code and I am able to alter the status of each led individually.

Link to comment
Share on other sites

the OP didn't actually change anything for the UPDATE queries from the code in reply #4. he is probably referring to the block of INSERT queries.

 

aoiregion, your current code, with all the if/else/ else if logic, ISN'T even running the code where your INSERT queries are at, because the condition being tested is false, because it's already been tested above that point in the code. you actually NEED to do the things i suggested to get your code to work, particularly item #4 of using a switch/case statement.

 

next, your INSERT queries are syntactically incorrect (insert queries don't have where clauses) and you need to use the techniques that Ch0cu3r and i have suggested so that you only have ONE query to run. again, i don't know why you have the insert queries at a separate point in the code - 

 

 

since your INSERT queries seem to be related to your UPDATE queries, why did you make a separate conditional branch? i think if you do item #4 on my list, your code will be greatly simplified (you will be able to see the forest, instead of just the trees.)

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.