Reputation: 85
This is my code for writing data to another database when the submit button is clicked. There are two buttons one accept and one deny, when accept is clicked it will write to a database called headforms and when deny is clicked, it will write the data to a database called deanforms. Please ignore the way i wrote the code because of my basic html/php/sql skills and it would be great if someone helped me!
<?php
} else if ($usertype == 1) {
$server = "localhost";
$user = "";
$pass = "";
$db = "";
$user1 = $_SESSION['username'];
$mysqli = new Mysqli($server, $user, $pass, $db) or mysqli_error($mysqli);
$overrides = $mysqli->query("SELECT * FROM Overrides WHERE professor = '$user1'");
$num_rows = mysqli_num_rows($overrides);
?>
<?php
echo " Overrides today: " . $num_rows;
?>
<?php
while($row = mysqli_fetch_array($overrides)) { ?>
<fieldset> <?php
echo "First Name: " . $row['name'] . "<br />";
echo "<br />Mid. Name: " . $row['mname'] . "<br />";
echo "<br />Fam. Name: " . $row['fname'] . "<br />";
echo "<br />Student ID: " . $row['sid'] . "<br />";
echo "<br />Scolarship: " . $row['sc'] . "<br />";
echo "<br />Phone No: " . $row['phone'] . "<br />";
echo "<br />Email: " . $row['email'] . "<br />";
echo "<br />Class: " . $row['class'] . "<br />";
echo "<br />Section: " . $row['section'] . "<br />";
echo "<br />Semester: " . $row['semester'] . "<br />";
$name = $row['name'];
$mname = $row['mname'];
$fname = $row['fname'];
$sid = $row['sid'];
$sc = $row['sc'];
$phone = $row['phone'];
$email = $row['email'];
$class = $row['class'];
$section = $row['section'];
$semester = $row['semester'];
?>
<br />
<div>
<label for="comments" accesskey="c">Notes & Comments:</label><br />
<textarea name="comments" cols="35" rows="10">
</textarea><br>
</div>
<br>
<form method="post" action="dbheads.php" name="HeadWritingForm" id="HeadWritingForm" autocomplete="off">
<input type="submit" class="submit" id="submit" value="Accept" /><br><br>
</form>
<form method="post" action="dbheads2.php" name="deny" id="deny" autocomplete="off">
<input type="submit" class="submit" id="submit" value="Deny" /><br><br>
</form>
</fieldset>
<br>
<?php }
?>
<br />
my dbheads.php
<?php
$mysql_host = "localhost";
$mysql_username = "";
$mysql_password = "";
$mysql_database = "";
$mysqli = new Mysqli($mysql_host, $mysql_username, $mysql_password, $mysql_database) or die(mysql_error());
$prepare = $mysqli->prepare("INSERT INTO `headforms`(`name`,`mname`,`fname`,`sid`,`email`,`phone`,`sc`,`class`,`section`,`semester`) VALUES (?,?,?,?,?,?,?,?,?,?)");
$prepare->bind_param("ssssssssss", $name, $mname, $fname, $sid, $email, $phone, $sc, $class, $section, $semester);
$prepare->execute();
if ($prepare) {
echo 'accepted';
} else {
echo 'sorry';
}
?>
my dbheads2.php
<?php
$mysql_host = "localhost";
$mysql_username = "";
$mysql_password = "";
$mysql_database = "";
$mysqli = new Mysqli($mysql_host, $mysql_username, $mysql_password, $mysql_database) or die(mysql_error());
$prepare = $mysqli->prepare("INSERT INTO `deanforms`(`name`,`mname`,`fname`,`sid`,`email`,`phone`,`sc`,`class`,`section`,`semester`) VALUES (?,?,?,?,?,?,?,?,?,?)");
$prepare->bind_param("ssssssssss", $name, $mname, $fname, $sid, $email, $phone, $sc, $class, $section, $semester);
$prepare->execute();
if ($prepare) {
echo 'denied';
} else {
echo 'Sorry';
}
?>
When accept is clicked, it prints on the page accepted and when deny is clicked, it prints denied but no writing to the database is done. Thanks in advance!
Upvotes: 0
Views: 51
Reputation: 498
Since Prepared
is not null, it sounds like you have a database issue. Something in the database is not allowing you to write to it.
What would be very handy to you is to see what is going on the database side.
MySQL has a great feature that allows you to view the queries that have been executed and has helped me multiple times.
SET GLOBAL log_output = 'TABLE';
SET GLOBAL general_log = 'ON';
Take a look at the table mysql.general_log
:
select * from mysql.general_log;
When you are done make sure you set general_log = 'OFF';
so that the log doesn't grow constantly.
I usually clear the log before I turn the log on:
truncate mysql.general_log;
Upvotes: 1
Reputation: 1923
You must set $user = ""; as root(default) or whatever you have and also provide the database name like $db="your database name" so php can know if in which databse the data should insert or to which database it need to interact.
Upvotes: 0