connect_error) die($conn->connect_error);
// Create database
$sql = "CREATE DATABASE IF NOT EXISTS $db";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $conn->error;
}
if (mysqli_select_db($conn,$db) === TRUE) {
echo "
connected to database successfully
";
} else {
echo "Error connecting to database: " . $conn->error;
}
$sql = "CREATE TABLE IF NOT EXISTS $table (
xyz_id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
rec_num INT(11) NOT NULL DEFAULT '0',
time_stamp INT(11) NOT NULL DEFAULT '0',
xval INT(11) NOT NULL DEFAULT '0',
yval INT(11) NOT NULL DEFAULT '0',
zval INT(11) NOT NULL DEFAULT '0',
note VARCHAR(30) NOT NULL
)";
/*
0 xyz_id
1 rec_num
2 time_stamp
3 xval
4 yval
5 zval
6 note
*/
if ($conn->query($sql) === TRUE) {
echo "table created successfully
";
} else {
echo "Error creating table: " . $conn->error;
}
/*
0 xyz_id
1 rec_num
2 time_stamp
3 xval
4 yval
5 zval
6 note
*/
//after user clicks delete, action is to run program with POST data. If POST data has a "delete" field and an ISBN field
// delete the record with the ISBN value, skip over the insert part and refresh the screen and wait for the next button press
if (isset($_POST['delete']) && isset($_POST['xyz_id']))
{
$xyz_id = get_post($conn, 'xyz_id');
$query = "DELETE FROM $table WHERE xyz_id='$xyz_id'";
$result = $conn->query($query);
if (!$result) echo "DELETE failed: $query
" .
$conn->error . "
";
}
// if the "delete" field was there all but the isbn field below will be empty and
//the insert section below will be skipped
// if all the fields have values, then insert those values as a new row, and
// then refresh the screen and wait for the next button press.
if (isset($_POST['rec_num']) &&
isset($_POST['xval']) &&
isset($_POST['yval']) &&
isset($_POST['zval']) &&
isset($_POST['note']))
{
$rec_num = get_post($conn, 'rec_num');
$xval = get_post($conn, 'xval');
$yval = get_post($conn, 'yval');
$zval = get_post($conn, 'zval');
$note = get_post($conn, 'note');
$time_stamp = time();
//$xyz_id = 1;
echo "
Just before insert command
";
$query = "INSERT INTO $table VALUES" .
"(
null,
'$rec_num',
'$time_stamp',
'$xval',
'$yval',
'$zval',
'$note'
)";
$result = $conn->query($query);
if (!$result) echo "INSERT failed: $query
" .
$conn->error . "
";
}
/*
0 xyz_id
1 rec_num
2 time_stamp
3 xval
4 yval
5 zval
6 note
*/
// Used sql "describe" command to show fields in table
$sql = "DESCRIBE $table";
$result = $conn->query($sql);
if (!$result) die ("Database access failed with describe: " . $conn->error);
$rows = $result->num_rows;
echo "
| $row[$k] | "; echo "
rec_num $row[1]
time_stamp $row[2]
xval $row[3]
yval $row[4]
zval $row[5]
note $row[6]
_END;
}
/*
0 xyz_id
1 rec_num
2 time_stamp
3 xval
4 yval
5 zval
6 note
*/
$result->close();
$conn->close();
function get_post($conn, $var)
{
return $conn->real_escape_string($_POST[$var]);
}
?>