inserting additional values immediately following a successful insert using PHP+MySQL - php

if(isset($_REQUEST['SAVE'])) {
$sql = "SELECT SecName FROM section WHERE SecID='$section'";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)){
$SecName = $row["SecName"];
}
$sql = "SELECT SubjName FROM subject WHERE SubjID='$subject'";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)){
$SubjName = $row["SubjName"];
}
$check = mysql_query("SELECT SecID FROM service where SubjID='$SubjName'")or die(mysql_error());
$bool = 1;
while($info = mysql_fetch_array( $check ))
if(($info['SecID'] == $SecName)){
$bool = 0;
}
if($bool){
$check = mysql_query("SELECT * FROM service ")or die(mysql_error());
$bool = 1;
while($info = mysql_fetch_array( $check ))
if(($info['Start_date'] == $Start_date) && ($info['Venue'] == $Venue) || ($info['Start_date'] == $Start_date) && ($info['Facilitator'] == $Facilitator)){
$bool = 0;
}
if($bool){
$sql="INSERT INTO service ( ServiceID, Date, Semester, School_year, Start_date, Venue, Facilitator, Stype, SecID, SubjID)VALUES('$RecCode','$Date','$Semester','$School_year','$Start_date','$Venue','$Facilitator','$Stype', '$SecName', '$SubjName')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo '<script type="text/javascript">';
echo 'alert("Save Successfully!");';
echo 'window.location="Admin_RecSchedMapLst.php";';
echo '</script>';
mysql_close($con);
}else
{
echo '<script type="text/javascript">';
echo 'alert("Conflicting schedule for Venue or Facilitator!");';
echo '</script>';
}
}else
{
echo '<script type="text/javascript">';
echo 'alert("The SECTION has been already scheduled!");';
echo '</script>';
}
}
/* $result = mysql_query("SELECT SIDno FROM class WHERE SecID=$SecID AND SubjID=$SubjID");
$row = mysql_fetch_assoc($result);
$SIDno = $row['SIDno'];
$result = mysql_query("SELECT ServiceID FROM service WHERE SecID=$SecID AND SubjID=$SubjID");
$row = mysql_fetch_assoc($result);
$ServiceID = $row['ServiceID'];
$sql="INSERT INTO registered ( ServiceID, IDno, Stype )VALUES('$ServiceID','$SIDno','$Stype')"; */
}
this code is working correctly,. i just want to ask how can i possibly work on the code whcih is commented above. all i want to do is when the serviceid is inserted i want also to immediately insert the serviceID, idno, type to another table which is registered.

It's much easier and better to use the mysql_insert_id function to get the ID of the row you just inserted, rather than doing another SELECT. E.g.:
$sql="INSERT INTO service ( ServiceID, Date, Semester, School_year, Start_date, Venue, Facilitator, Stype, SecID, SubjID) VALUES ('$RecCode','$Date','$Semester','$School_year','$Start_date','$Venue','$Facilitator','$Stype', '$SecName', '$SubjName')";
if (mysql_query($sql,$con)) {
$ServiceID = mysql_insert_id();
$sql="INSERT INTO registered ( ServiceID, IDno, Stype )VALUES('$ServiceID','$SIDno','$Stype')";
mysl_query($sql, $con); // check for error here too though
}
else {
echo "OH NO!!!";
}
Also, beware of SQL injection with the way you're building your SQL strings.

Execute another SQL INSERT command after the first one, like:
$sql="INSERT INTO service ( ServiceID, Date, Semester, School_year, Start_date, Venue, Facilitator, Stype, SecID, SubjID) VALUES ('$RecCode','$Date','$Semester','$School_year','$Start_date','$Venue','$Facilitator','$Stype', '$SecName', '$SubjName')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
else
{
/*Begin your SELECT and INSERT code*/
$result = mysql_query("SELECT SIDno FROM class WHERE SecID=$SecID AND SubjID=$SubjID");
$row = mysql_fetch_assoc($result);
$SIDno = $row['SIDno'];
$result = mysql_query("SELECT ServiceID FROM service WHERE SecID=$SecID AND SubjID=$SubjID");
$row = mysql_fetch_assoc($result);
$ServiceID = $row['ServiceID'];
$sql2 = "INSERT INTO registered ( ServiceID, IDno, Stype )VALUES('$ServiceID','$SIDno','$Stype')";
/*End your SELECT and INSERT code*/
if(!mysql_query($sql2,$con))
{
die('Error: ' . mysql_error());
}
}

Related

PHP new strings update in DB with unshift?

I have a problem to adding more strings in my database.
The idea is: SELECT information, then added array together, after these UPDATE to database.
These are in one code, but UPDATE not working with summed arrays only separately.
With echo I see the array_unshift is working well, the data is good, but not updating.
Need I change something on the server? Maybe version?
(I don't get mysqli_error!)
//CHECKBOX KIOLVASÁSA DB-BŐL!
$sql = ("SELECT id, checkbox FROM osszesito WHERE id = '$id'");
//$result = mysqli_query($conn, $sql);
//if (mysqli_num_rows($result) > 0) {
if ($result = mysqli_query($conn, $sql)) {
while($row = mysqli_fetch_assoc($result)) {
//EREDETI SOR LISTÁZÁSA
$original_array = array( $row["checkbox"] );
$x ="";
echo 'Eredeti sor: ';
foreach ($original_array as $x)
{
echo "$x "."<br><br>";
}
//EREDETI SOR KIEGÉSZÍTÉSE AZ ÚJ ADATTAL
array_unshift($original_array, $chb);
$last ="";
echo "Új sor: "."<br>";
foreach ($original_array as $last)
{
echo $last."<br>";
}
//ÚJ SOR FRISSÍTÉSE A DB-BEN!
//$sqla = "UPDATE osszesito SET checkbox = '$chb' WHERE id = '$id' ";
$sqla = "UPDATE osszesito SET checkbox = '$last' WHERE id = '$id' ";
if (mysqli_query($conn, $sqla)) {
echo "ÚJ SOR ELMENTVE!";
//header("Location: /megrendelesek/index.php");
} else {
echo "Hiba a beírás során: " . mysqli_error($conn);
}
}
///////////////////////////////////////////////
//LEZÁRÁS
} else {
echo "Jelenleg nincs megrendelés az adatbázisban!";
}
mysqli_close($conn);

php sql Variable

Trying to load $location into the sql query but just returning Invalid:
//query to Equipment
$Location= $_GET["id"];
$sql = mysqli_query($con,"SELECT * FROM `Equipment` WHERE `Location` = `".$Location."`");
$result = $sql;
if (!$result)
{
die('Invalid query: ' . mysqli_error());
}
while ($row = mysqli_fetch_array($result))
{
The $_GET['id'] is called from another page. I double checked link says id
#Dave Baker try something like below :
<?php
$Location = isset($_GET["id"]) ? $_GET["id"] : 0;
if($Location){
$sql = mysqli_query($con,"SELECT * FROM Equipment WHERE Location = $Location"); //if location type is integer else use below one
$sql = mysqli_query($con,"SELECT * FROM Equipment WHERE Location = '$Location'"); //if location type is string
$result = $sql;
if (!$result)
{
die('Invalid query: ' . mysqli_error());
}
while ($row = mysqli_fetch_array($result))
{}
}
else{
echo "id is not set";
}

inserting record into mysql table if column count value is 2

Below is code for count record for today's date and inserting into database if value is not more than two per day.now problem is I want to insert into database if count for today is below 2.
$user_ip = getenv('REMOTE_ADDR');
$geo = unserialize(file_get_contents("http://www.geoplugin.net/php.gp?ip=$user_ip"));
$city = $geo["geoplugin_city"];
$region = $geo["geoplugin_regionName"];
$img = $_POST['img'];
$amount = 5;
$sql = "SELECT COUNT(*) FROM `daily_uploads` WHERE DATE_FORMAT(`date`, '%Y-%m-%d') = CURDATE()";
$result = $conn->query($sql);
if ($result->num_rows > 2) {
echo"already exist";
echo "Error: " . $sql . "<br>" . $conn->error;
} else {
$sql = "INSERT INTO `daily_uploads` (img, geoplugin_city, geoplugin_regionName, amount)
VALUES ('$img', '$city', '$region','$amount')";
// echo "success";
}
You have forgotten to execute the insert $sql string, you can do it this way:
if ($conn->query($sql)) {
echo ('success');
} else {
echo ('error');
}

if cells are null insert syntax

I'm trying to check whether major, grade and university in candidates table, are empty, if so then insert in university...Else...
Is my syntax appropriate?
$sqlCheck1 = "SELECT `Major`, `Grade`, `University` FROM Candidates WHERE ID='".$_GET["cid"]."'";
$result5 = mysqli_query($con,$sqlCheck1);
while($row5 = mysqli_fetch_array($result5)) {
$major = $row5['Major'];
$grade = $row5['Grade'];
$university = $row5['University'];
if (mysqli_num_rows($result5) == 0)
{
$sql5 = "INSERT INTO `university` (`major`, `degree`, `univ`, `afnumber`) VALUES ('$major','$grade','$university','".$_GET["af"]."')";
if (mysqli_query($con,$sql5) === TRUE) {
} else {
echo "Error: " . $sql5 . "<br>" . mysqli_error($con);
}
}
else
{
Use the follwing code
$sqlCheck1 = "SELECT `Major`, `Grade`, `University` FROM Candidates WHERE ID='".$_GET["cid"]."'";
$result5 = mysqli_query($con,$sqlCheck1);
if (mysqli_num_rows($result5) == 0)
{
$sql5 = "INSERT INTO `university` (`major`, `degree`, `univ`, `afnumber`) VALUES ('$major','$grade','$university','".$_GET["af"]."')";
if (mysqli_query($con,$sql5) === TRUE) {
} else {
echo "Error: " . $sql5 . "<br>" . mysqli_error($con);
}
}
else
{
well you are saying that if major, grade and university are empty than insert those empty values in university but the question here is why you want to enter those values if they are empty, even if you want to do so along with inserting afnumber using "$_GET["af"]" variable than you can use following code..
$sqlCheck1 = "SELECT `Major`, `Grade`, `University` FROM Candidates WHERE ID='".$_GET["cid"]."'";
$result5 = mysqli_query($con,$sqlCheck1);
if (mysqli_num_rows($result5) == 0)
{
$sql5 = "INSERT INTO `university` (`afnumber`) VALUES ('".$_GET["af"]."')";
if (mysqli_query($con,$sql5) === TRUE) {
} else {
echo "Error: " . $sql5 . "<br>" . mysqli_error($con);
}
}
its quite short and fulfill the purpose but make sure you have checked null in database for major, grade and univ fields in university table .

Not getting sum of rows from table

The Results of <span>$invoicepartpaid1</span> and <span>$invoicepartpaid2</span> are blank. There should be sums. Any suggestions?
$query="SELECT Distinct company, car_moto, packet FROM companies where userid='$userid' ORDER BY company, car_moto, packet";
$result=mysql_query($query);
$num=mysql_numrows($result);
$i=0;
while ($i < $num) {
$f0=mysql_result($result,$i,"company");
$f1=mysql_result($result,$i,"car_moto");
$f2=mysql_result($result,$i,"packet");
$sql_1 = "SELECT SUM(price_protect) as partpaid1 FROM companies where company='$f0' and car_moto='$f1' and packet='$f2' and userid='$userid'";
$sql_2 = "SELECT SUM(clear_protect) as partpaid2 FROM companies where company='$f0' and car_moto='$f1' and packet='$f2' and userid='$userid'";
$result_1=mysql_query($sql_1) or die('Error query failed');
$result_2=mysql_query($sql_2) or die('Error query failed');
while ($row_1 = mysql_fetch_array($result_1)) {
$invoicepartpaid1 = $row_1['partpaid1'];
}
while ($row_2 = mysql_fetch_array($result_2)) {
$invoicepartpaid2 = $row_2['partpaid2'];
}
echo "<tr><td align='center'><span>$f0</span></td><td align='center'><span>$f1</span></td><td align='center'><span>$f2</span></td>";
echo "<td align='center'><span>$invoicepartpaid1</span></td><td align='center'><span>$invoicepartpaid2</span></td></tr>";
$i++;
}
Replace the line
$num=mysql_numrows($result);
By
$num=mysql_num_rows($result);
Full Code
<?php
$query="SELECT Distinct company, car_moto, packet FROM companies where userid='$userid' ORDER BY company, car_moto, packet";
$result=mysql_query($query);
$num=mysql_num_rows($result);
$i=0;
while ($row=mysql_fetch_array($result)) {// loop for all results replacing $i < $num
$f0=$row["company"];
$f1=$row["car_moto"];
$f2=$row["packet"];
$sql_1 = "SELECT SUM(price_protect) as partpaid1 FROM companies where company='$f0' and car_moto='$f1' and packet='$f2' and userid='$userid'";
$sql_2 = "SELECT SUM(clear_protect) as partpaid2 FROM companies where company='$f0' and car_moto='$f1' and packet='$f2' and userid='$userid'";
$result_1=mysql_query($sql_1) or die('Error query failed');
$result_2=mysql_query($sql_2) or die('Error query failed');
$invoicepartpaid1=0;$invoicepartpaid2=0;
while ($row_1 = mysql_fetch_array($result_1)) {
$invoicepartpaid1 = $row_1['partpaid1'];
}
while ($row_2 = mysql_fetch_array($result_2)) {
$invoicepartpaid2 = $row_2['partpaid2'];
}
echo "<tr><td align='center'><span>$f0</span></td><td align='center'><span>$f1</span></td><td align='center'><span>$f2</span></td>";
echo "<td align='center'><span>$invoicepartpaid1</span></td><td align='center'><span>$invoicepartpaid2</span></td></tr>";
$i++;
}
?>

Categories