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 ""; for ($j = 0 ; $j < $rows ; ++$j) //nixon pg 249 { $result->data_seek($j); $row = $result->fetch_array(MYSQLI_NUM); echo ""; for ($k = 0 ; $k < 4 ; ++$k) echo ""; echo ""; } echo "
$row[$k]
"; //after either the delete action or the insert action has occurred, the program comes to here and //does all the steps below, giving "submit" buttons, one for each delete and one for insert. echo <<<_END
    
    rec_num 
    xval  
	yval 
    zval 
    note 
    
  
_END; $query = "SELECT * FROM $table"; $result = $conn->query($query); if (!$result) die ("Database access failed: " . $conn->error); $rows = $result->num_rows; //the loop below creates a form, with a DELETE button for each record. Each form has its own data such that // the delete button will delete the record with ISBN # put in the hidden field in that form for ($j = 0 ; $j < $rows ; ++$j) { $result->data_seek($j); $row = $result->fetch_array(MYSQLI_NUM); echo <<<_END
     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]); } ?>