this is my php code. hire is a problem i use check box selection and insert data in my database. but it insert data by foreach loop. so when press ok if data successfully insert then for 3 selection it say
successfully register
successfully register
successfully register
but i want it say only once successfully register
<?php
$db=require "script_database/connect.php";
$query = "SELECT * FROM course";
$query1="select * from selection where student_id='1229CSE00241' and semester='FALL2015' ";
$key=mysql_query($query1);
if(mysql_num_rows($key)>0)
{
echo "you already selected courses for registration";
}
else if($_POST['buy']==''){
echo "<h2><center>You didn't select any courses</h2></center>";
}
else{
foreach($_POST['buy'] as $item) {
$query = "SELECT * FROM course WHERE id = $item
";
if ($r = mysql_query($query)) {
while ($row = mysql_fetch_array($r)) {
$student_id="1229CSE00241";
$id=$item;
$course_id=$row['course_id'];
$course_title=$row['course_title'];
$course_credits=$row['course_credits'];
$course_status=$row['course_status'];
$semester="FALL2015";
}
} else {
print '<p style="color: blue">Error!</p>';
}
{
$insert_query="insert into selection(student_id,semester,course_id,course_title,course_credits,course_status,date_time) values ('$student_id','$semester','$course_id','$course_title','$course_credits','$course_status',NOW())";
}
//here is my problem
//it repeat every time when insert data but i want to make it only once
if(mysql_query($insert_query))
{
echo "successfully register";
}
else
echo "problem show";
}
}?>
Before you start your foreach loop set a flag like:
$error = false;
Then in your loop
if(!mysql_query($insert_query))
{
$error =true;
}
And after the loop has closed
if($error){
echo "problem show";
}else{
echo "successfully register";
}
Related
I am creating a result page using php to pull data from database.
I have been able to connect the database to my webspace and displayed the data.
Now , I would like to have the page not show all the data at once. I want to add an input field example "Origen= Los Angeles" "Destination = London" and then show the results based on that criteria.
This is my first time doing something like this so if this sounds like I should know this I'm sorry. I hope I can get some help.
<?php
// include connection settings
require_once "connect.php";
// display a list of flights
if(!empty($_GET) && !empty($_GET['name'])){
$query = "SELECT * FROM Flight_Information WHERE name LIKE ''".
$_GET['name']."".$_GET['name']."'";
} else {
$query = "SELECT ID, airlineName, departureAirport, departureDate, destinationAirport FROM Flight_Information";
} // END if
$result = $db->query($query);
if ($result) {
// ... display results in while loop
while ($row = $result->fetch_assoc()) {
echo '<li>'.$row['airlineName'].' '.$row['departureAirport'].''.$row['departureDate'].''.$row['destinationAirport'].'</li>';
} // END while
} else {
// if there was an error with your query
// this will display it
echo "SQL Error: " . $db->error;
} // END if
?>
</ul>
<?php
if (!empty($_GET) && !empty($_GET['Flight_Information_ID'])) {
$Flight_Information_ID = $_GET['Flight_Information_ID'];
$query = "SELECT * FROM Flight_Information WHERE ID =" . $Flight_Information_ID;
// perform the query
if ($result = $db->query($query)) {
// check we have a result
if ($result->num_rows > 0) {
// loop through and print out the results
while ($row = $result->fetch_assoc()) {
// output a title for each result
echo '<hr/><p>You have selected FriendShipper' .$Flight_Information_ID. ':</p> <ul>';
// loop through and output details for each item
foreach ($row as $key => $value) {
echo '<li><em>'.$key.'</em> = <strong>'.$value.'</strong></li>';
}
// output a horizontal rule under the result
echo '</ul><hr/>';
}
// num_rows = 0, no results found
} else {
echo "<p>No Flights Found " .$Flight_Information_ID. "</p>";
}
// if there was an error with your query, this will display it
} else {
echo "SQL Error: " . $db->error;
}
}
?>
I want to ask about checking a value in the database: when the value exist it will echo an alert message and when not it will do an insert function.
I have try a code like this but it seems not work.
<?php
include("../../Connections/koneksi.php");
if(isset($_POST['table-bordered'])){
$array=json_decode($_POST['table-bordered'],true);
foreach($array as $item) {
$sql = "SELECT * FROM wjm WHERE no_pol='".$item['no_pol']."',date='".$item['date']."',time='".$item['time']."'";
$query = mysqli_query($db,$sql);
if (mysqli_num_rows($query)> 0)
{
echo "Data sudah pernah di Input..!!";
}
else{
$sql = "INSERT INTO wjm (sloc,kode,nama,no_pol,id,date,time,netto,unit,uses,payroll) VALUES ('".$item['sloc']."', '".$item['kode']."', '".$item['nama']."', '".$item['no_pol']."', '".$item['id']."', '".$item['date']."', '".$item['time']."', '".$item['netto']."', '".$item['Unit']."', '".$item['uses']."', '".$item['payroll']."')";
if(mysqli_query($db, $sql)){
echo "Records inserted successfully.";
} else{
echo "Records inserted failed ";
}
}
}
}
?>
I want to check the value in my select function. when it exist in my table in will echo an alert in the page. I have try whit select count too but it not working
I have a mysql database that I am working on. The last part of the work is to send nomination data to different tables based on the zone the user comes from.
There are tree zones (1, 2 and 3) depending on the country you live in.
What I want to do is enter user nomination into 3 different table based on the zone they belong.
I have tried using the if statement but it's not working for me. The only part that enters a value is when the user is in zone 3 (America) that value is sent to the america table. For the other users it does not work.
Here is my current code:
if(isset($_POST['submit']))
{
#$email=$_POST['email'];
$president=$_POST['president'];
#$vice_president=$_POST['vice_president'];
#$secretary=$_POST['secretary'];
#$treasurer1=$_POST['treasurer1'];
#$treasurer2=$_POST['treasurer2'];
#$treasurer3=$_POST['treasurer3'];
#$date= date("Y-m-d h:i:sa");
#$voter=$_SESSION['email'];
$sql = "SELECT zone FROM nickdel_db.students
WHERE email='".$_SESSION['email']."'";;
$result = mysqli_query($con, $sql) or die('error getting zone');
$rs=mysqli_fetch_array($result);
if($rs[0]==1)
{
$query = "INSERT INTO nickdel_db.africavote values(Null,'$email','$president','$vice_president','$secretary','$treasurer1','$treasurer2','$treasurer3','$date','$voter')";
$query_run = mysqli_query($con,$query);
if($query_run)
{
echo '<script>alert("You have succefuly nominated executives");</script>';
function redirect ($location,$delay=0) {
echo "<meta http-equiv='refresh' content='$delay; url=$location' />";
}
redirect('https://nci99set.com/homepage.php');
exit;
}else{
echo '<p class="bg-danger msg-block">Registration Unsuccessful due to server error.</p>';
}
}elseif($rs[0]==2){
$query = "INSERT INTO nickdel_db.europevote values(Null,'$email','$president','$vice_president','$secretary','$treasurer1','$treasurer2','$treasurer3','$date','$voter')";
$query_run = mysqli_query($con,$query);
if($query_run){
echo '<script>alert("You have succefuly nominated executives");</script>';
function redirect ($location,$delay=0) {
echo "<meta http-equiv='refresh' content='$delay; url=$location' />";
}
redirect('https://nci99set.com/homepage.php');
exit;
}else{
echo '<p class="bg-danger msg-block">Registration Unsuccessful due to server error.</p>'; }
}else{
$query = "INSERT INTO nickdel_db.americavote values(Null,'$email','$president','$vice_president','$secretary','$treasurer1','$treasurer2','$treasurer3','$date','$voter')";
$query_run = mysqli_query($con,$query);
if($query_run){
echo '<script>alert("You have succefuly nominated executives");</script>';
function redirect ($location,$delay=0) {
echo "<meta http-equiv='refresh' content='$delay; url=$location' />";
}
redirect('https://nci99set.com/homepage.php');
exit;
}else{ echo '<p class="bg-danger msg-block">Registration Unsuccessful due to server error.</p>';
}
}
}
else
{
}
Ok, so I have this in the top part of my code
require(__DIR__ . "/lib/db.php");
if (!empty($_POST["action"])) {
if ($_POST["action"] == "addCat") {
echo "Please wait...";
$sql1 = "INSERT INTO categories (name) VALUES ('".$_POST["name"]."')";
if (mysql_query($sql1)) {
$success = true;
}
if (!isset($success)) {
echo "Error Occured Adding Category!";
} else {
echo "Category added!";
}
}
if ($_POST["action"] == "delCat") {
echo "Please wait...";
$catNames = $_POST['catName'];
if(empty($catNames)) {
return;
} else {
$N = count($catNames);
for($i=0; $i < $N; $i++) {
$sql = "DELETE FROM categories WHERE id='$catNames[$i]'";
if (mysql_query($sql)) {
$success = true;
}
$sql2 = "SELECT * FROM catrel WHERE categoryID = '$catNames[$i]'";
if (mysql_query($sql2)) {
mysql_query("DELETE FROM catrel WHERE categoryID = '$catNames[$i]'");
}
}
}
if (!isset($success)) {
echo "Could not delete category! Error Occured!";
} else {
echo "Category deleted!";
}
}
}
I have a 2 different forms in the same page, with hidden values that will push addCat or delCat. For some reason it seems like the SQL is not getting parsed, however it is not displaying any errors. Any help would be highly appreciated. I can post the entire page if needed.
im having a little problem getting the if($stmt->errorCode() == 0) { in my code to work. If i do a search with a ticket number that is in the database it will show it but if i do a search with a random number that's not in the database it will not show the error message no ticket found.
<?php
require("db.php");
$error_message="";
if (isset($_POST['submit'])){
if(empty($_POST['term']))
{
$error_message="Please enter a Ticket Number.";
}
else
{
$query = "SELECT department, subject, message FROM supporttickets Where ticketnumber LIKE :term";
$stmt = $db->prepare($query);
$stmt->execute(array(':term' => $_POST['term']));
if($stmt->errorCode() == 0) {
while (list($department,$subject,$message) = $stmt->fetch(PDO::FETCH_NUM)) {
echo htmlentities($department);
}
}else{
$error_message="no ticket found.";
}
}
}
?>
Your problem is that you're not actually echoing the output. This just assigns a variable:
$error_message="no ticket found.";
What you want is:
$error_message="no ticket found.";
echo $error_message;
Or maybe just:
echo "no ticket found.";
You don't need no error codes here
<?php
require("db.php");
$data = array();
if(!empty($_GET['term']))
{
$query = "SELECT department, subject, message FROM supporttickets
WHERE ticketnumber = ?";
$stmt = $db->prepare($query);
$stmt->execute(array($_GET['term']));
$data = $stmt->fetchAll();
}
foreach ($data as $row)
{
echo htmlspecialchars($row['department']);
}
if (!$data)
{
echo "Please enter valid Ticket Number.";
}
A couple of mistakes also corrected.