我在表格中添加了一个外键。现在,当我尝试将表单中的数据插入数据库时,它不起作用。我认为问题可能出在名为' register_id'的外键上。如何解决这个问题?
我的代码:
<?php
$username = "root";
$password = "";
$hostname = "localhost";
$dbhandle = mysql_connect($hostname, $username, $password) or die("cannot connect to database");
$selected = mysql_select_db("audit", $dbhandle);
$strQuery = 'SELECT * FROM schedule';
$retval = mysql_query($strQuery, $dbhandle);
if (!$retval) {
die('Could not get data: ' . mysql_error());
}
//insert code
if (isset($_POST['schedule'])) {
//$id = ($_POST['id']);
$_SESSION['company_session'] = $_POST['company_name'];
$_SESSION['auditor_session'] = $_POST['auditor_name'];
$_SESSION['date_session'] = $_POST['audit_date'];
$_SESSION['time_session'] = $_POST['audit_time'];
$_SESSION['status_session'] = $_POST['audit_status'];
$company_session = $_SESSION['company_session'];
$auditor_session = $_SESSION['auditor_session'];
$date_session = $_POST['audit_date'];
$time_session = $_POST['audit_time'];
$status_session = $_SESSION['status_session'];
mysql_query("INSERT INTO schedule (company_name,auditor_name,audit_date,audit_time,audit_status) VALUES ('$company_session','$auditor_session','$date_session','$time_session','$status_session')");
//echo ("New Process Created successfully");
header('Location: ?');
}
?>
// html代码
<div class="form-group"><br>
<table class="table">
<tr><input class="form-control" type="hidden" name="id"></tr>
<tr><input class="form-control" type="hidden" name="register_id"></tr>
<?php
$connect = mysql_connect("localhost", "root", "") or die("Could not connect to the database");
mysql_select_db("audit") or die("could not find db");
$query = mysql_query("SELECT fullname FROM register WHERE register_id ='" . $_SESSION['register_id'] . "'");
$numrows = mysql_num_rows($query);
if ($numrows != 0) {
$row = mysql_fetch_assoc($query);
$name = $row['fullname'];
}
?>
<tr>
<td><label>Company Name:</label></td>
<td><input class="form-control" type="text" name="company_name" required="true"></td>
<td><label>Assigned Auditor:</label></td>
<?php echo "<td><input class='form-control' name='auditor_name' value='$name' required='true'/></td>" ?>
</tr>
<tr>
<td><label>Scheduled Date:</label></td>
<td><input class="form-control" type="date" name="audit_date" min="2000-01-02" required="true"></td>
<td><label>Scheduled Time:</label></td>
<td><input class="form-control" type="time" name="audit_time" required="true"></td>
</tr>
<tr>
<td><label>Status:</label></td>
<td><input class="form-control" type="text" name="audit_status" value="Not started" required="true"></td>
<td></td>
<td><button class="btn btn-primary" type="submit" name="schedule" value="submit">Schedule</button> <input class="btn btn-primary" type='submit' value='Update' name='update' /> <input class="btn btn-primary" type='submit' value='Delete' name='delete' /> <input class="btn btn-primary" type='submit' value='Reset' name='reset' /></td>
</tr>
</div>
</table>
</div>
</form>