I'm currently working on a database where I would like the order_ID's to be put into two tables. This works but the issue I have is that the loop is only iterating once. If anyone could help and explain where I have gone wrong it would be greatly appreciated.
session_start();
echo $_SESSION['shop_id'];
$shopid = $_SESSION['shop_id'];
$username = $_SESSION['username'];
$con = mysqli_connect('localhost', 'root', '', 'aurora');
if (!isset($con)) {
echo "Connection to Aurora System failed.";
}
if (isset($_POST['items'])) {
echo "True";
} else {
echo "false";
}
$valid = true;
$date = date('l jS \of F Y h:i:s A');
$sql2 = "INSERT INTO orders_new (user_submitted, order_date, customer_ID) VALUES ('$username', '$date', '$shopid')";
if ($valid == true) {
$ordersubmit2 = mysqli_query($con, $sql2);
echo "Success!";
}
$count = $_POST['items'];
for ($i = 1; $i <= $count; $i++) {
$idinsert = mysqli_insert_id($con);
$product = $_POST['product'.$i];
$nicotine = $_POST['nicotine'.$i];
$qty = $_POST['qty'.$i];
echo $product;
$sql = "INSERT INTO orders_detail (orders_id ,product, variant, quantity) VALUES ('$idinsert', '".$product."', '".$nicotine."', '".$qty."')";
$ordersubmit = mysqli_query($con, $sql);
}
Was my own fault, the order_id was a primary key hence the loop stopping due to only allowing unique values.
Related
This question already has answers here:
How to check if a row exists in MySQL? (i.e. check if username or email exists in MySQL)
(4 answers)
Closed 5 years ago.
I am using the code below i need to check the description must not be same so i have to check the description column for duplicate entry how to do this ?
<?php
session_start();
require 'Admin/includes/connection.php';
$emp_id = $_SESSION['id'];
$task_name = $_POST['task_name'];
$desc = $_POST['desc'];
$hours = $_POST['hours'];
$t = "random";
$a = date('Y-m-d');
$sql = "INSERT INTO project_task (emp_id, task_type, task, description, hours, submit_date) VALUES ('$emp_id', '$t', '$task_name', '$desc', '$hours', '$a')";
if($conn->query($sql) === TRUE){
$data["abc"] = "true";
}
else{
$data["abc"] = "false";
}
header("Content-type: application/json");
echo json_encode($data);
?>
This Will Help You
<?php
session_start();
require 'Admin/includes/connection.php';
$emp_id = $_SESSION['id'];
$task_name = $conn->real_escape_string($_POST['task_name']);
$desc = $conn->real_escape_string($_POST['desc']);
$hours = $_POST['hours'];
$t = "random";
$a = date('Y-m-d');
$q = "SELECT description from project_task where description = '$desc' ";
$result = $conn->query($q);
if($result->num_rows > 0 ){
$data["result"] = "false";
}else{
$sql = "INSERT INTO project_task (emp_id, task_type, task, description, hours, submit_date) VALUES ('$emp_id', '$t', '$task_name', '$desc', '$hours', '$a')";
if($conn->query($sql) === TRUE){
$data["result"] = "true";
}
else{
$data["result"] = "false";
}
}
header("Content-type: application/json");
echo json_encode($data);
?>
I'm making a form for some project. The problem is when the user enter their data using the field and then submit, the input not keep in the database.
Also when clicking submit the direct page show blank empty page even the connection test also not showing.
I'm using almost similar code for other project and it work except for this.
below is my code:
<?php
//check connection
require 'config.php';
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
//asas (table name)
$id = $_POST["Sid"]; $ic = $_POST["Sic"];
$name = $_POST["Snp"]; $jant = $_POST["J1"];
$trum = $_POST["Chr"];$tbim = $_POST["Chp"];
$mel = $_POST["Sem"]; $arum = $_POST["Ar"];
$asum = $_POST["As"];
//institusi
$thp = $_POST["T1"]; $uni = $_POST["Sis"];
$bid = $_POST["tpe"];$Aint = $_POST["Ai"];
//industri
$bip = $_POST["bid"];$bik = $_POST["B1"];
$tem = $_POST["te"];$mula = $_POST["tm"];
$tamm = $_POST["tt"]; $res = $_POST["fileToUpload1"];
$tran = $_POST["fileToUpload2"];$keb = $_POST["fileToUpload3"];
$link = mysqli_connect($h,$u,$p,$db);
if('id' != '$Sid'){
$asas = "insert into asas Values ('$id','$ic','$name','$jant','$trum','$tbim','$mel','$arum','$asum')";
$inst = "insert into institusi Values ('$thp','$uni','$bid','$Aint')";
$indr = "insert into industri Values ('$bip','$bik','$tem','$mula','$tamm','$res','$tran','$keb')";
mysqli_query($link,$asas);
mysqli_query($link,$inst);
mysqli_query($link,$indr);
mysqli_close($link);
}
else
{
echo "failed"
}
?>
<b>Register complete</b>
Can anybody tell me what the error or maybe some solution. Thanks
I think you are having problem in insert query, please check this:
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john#example.com')";
Write this kind.
thank you
there are few issues with the code like variable id was used without $
and need to use die method with mysqli_query() function to check for
errors, please check below improved codes, it may help you -
<?php
//check connection
require 'config.php';
if (isset($_POST)) {
//asas (table name)
$id = $_POST["Sid"];
$ic = $_POST["Sic"];
$name = $_POST["Snp"];
$jant = $_POST["J1"];
$trum = $_POST["Chr"];
$tbim = $_POST["Chp"];
$mel = $_POST["Sem"];
$arum = $_POST["Ar"];
$asum = $_POST["As"];
//institusi
$thp = $_POST["T1"];
$uni = $_POST["Sis"];
$bid = $_POST["tpe"];
$Aint = $_POST["Ai"];
//industri
$bip = $_POST["bid"];
$bik = $_POST["B1"];
$tem = $_POST["te"];
$mula = $_POST["tm"];
$tamm = $_POST["tt"];
$res = $_POST["fileToUpload1"];
$tran = $_POST["fileToUpload2"];
$keb = $_POST["fileToUpload3"];
}
$link = mysqli_connect($h, $u, $p, $db);
if (!$link) {
die("Connection failed: " . mysqli_connect_error());
}
// if('id' != '$Sid'){
if ($id != '$Sid') {
$asas = "insert into asas Values
('$id','$ic','$name','$jant','$trum','$tbim','$mel','$arum','$asum')";
$inst = "insert into institusi Values ('$thp','$uni','$bid','$Aint')";
$indr = "insert into industri Values
('$bip','$bik','$tem','$mula','$tamm','$res','$tran','$keb')";
if (mysqli_query($link, $asas)) {
echo "records inserted";
} else {
echo "failed".mysqli_error($link) ;
}
if (mysqli_query($link, $inst)) {
echo "records inserted";
} else {
echo "failed".mysqli_error($link) ;
}
if (mysqli_query($link, $indr)) {
echo "records inserted";
} else {
echo "failed".mysqli_error($link) ;
}
}
mysqli_close($link);
?>
<b>Register complete</b>
just use or die after mysqli_query
mysqli_query($link,$asas)or die ('Unable to execute query. '. mysqli_error($link));
you will get to know what is the actual problem
just want to add record if count of column is less than 2 for today's date and if count is more than two it should not get insert into the db.It's keep getting added after two records.
$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(*) as totalupload FROM `daily_uploads` WHERE DATE_FORMAT(`date`, '%Y-%m-%d') = CURDATE()";
$row = mysqli_fetch_assoc($sql);
$sum = $row['totalupload'];
if ($sum < 2 ) {
$sql = "INSERT INTO `daily_uploads` (img, geoplugin_city, geoplugin_regionName, amount)
VALUES ('$img', '$city', '$region','$amount')";
if ($conn->query($sql)) {
echo ('success');
} else {
echo ('error');
}
} else {
echo"already exist";
make the connection after count query like this,
$result = mysqli_query($con,$sql);
Try this one hope it will help you.
$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(*) as totalupload FROM `daily_uploads` WHERE DATE_FORMAT(`date`, '%Y-%m-%d') = CURDATE()";
$qry= mysql_query($sql);
$row = mysql_fetch_assoc($qry);
$count = $row['totalupload'];
if ($count < 2 ) {
$sql = "INSERT INTO `daily_uploads` (img, geoplugin_city, geoplugin_regionName, amount)
VALUES ('$img', '$city', '$region','$amount')";
if ($conn->query($sql)) {
echo ('success');
} else {
echo ('error');
}
} else {
echo"already exist";
<?php
//$game_id = ['gameid'];
$first_name = $_POST['firstname'];
$last_name = $_POST['lastname'];
$address = $_POST['address'];
$postal_code = $_POST['postalcode'];
$start_date = $_POST['startdate'];
$rental_days = $_POST['rentaldays'];
$borrow_game = $_POST['borrowgame'];
Variables are set here^!
$end_date = date('Y-m-d', strtotime($start_date.' + '.$rental_days.'days'));
How the end_date function works^!
$conn = mysqli_connect("localhost", "evansbwg_user", "1password1", "evansbwg_database");
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
Connection^!
$result = mysqli_query($conn, "SELECT * FROM tbl_games WHERE game_name = '$borrow_game'");
//$result_present = mysqli_query($conn, $result);
$num_rows = mysqli_num_rows($result);
if ($num_rows == 1)
{
$row2 = mysqli_fetch_array($result);
$id = $row2['game_id'];
Getting game_id to equal borrow_game^!
$sql_search = "SELECT * FROM tbl_rental WHERE game_id = '$id'
AND (start_date <= '$start_date' AND end_date >= '$end_date')
OR (start_date >= '$start_date' AND start_date <= '$end_date')
OR (end_date >= '$start_date' AND end_date <= '$end_date')
OR (start_date >= '$start_date' AND end_date <= '$end_date')";
$result1 = mysqli_query($conn, $sql_search);
$num = mysqli_num_rows($result1);
if ($num == 0)
{
$sql = "INSERT INTO tbl_rental (game_id, first_name, last_name, address, postal_code, start_date, rental_days, borrow_game, end_date)
VALUE ('$id', '$first_name', '$last_name', '$address', '$postal_code', '$start_date', '$rental_days', '$borrow_game', '$end_date')";
Inserting the data into the table^!
if (mysqli_query($conn, $sql))
{
echo "New record created successfully";
}
else
{
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
}
while ($row = mysqli_fetch_array($result))
{
$id = $row['game_id'];
}
mysqli_close($conn);
?>
I have fixed my main problem however if any changes need to be made please suggest.
You closed your connection prematurely, so the query following that has no connection to the query.
mysqli_close($conn);
^^^^^^^^^^^^^^^^^^^^
}
while ($row = mysqli_fetch_array($result))
{
$id = $row['game_id'];
}
place it at the end of your script or just remove it.
Also this line makes no sense $end_date = ['enddate'];
You probably meant to do:
$end_date = $_POST['enddate'];
since the others above that are using POST arrays.
However, seeing
$end_date = date('Y-m-d', strtotime($start_date.' + '.$rental_days.'days'));
You would need to remove $end_date = ['enddate'];, that's invalid syntax.
Sidenote:
Your present code is open to SQL injection. Use prepared statements, or PDO with prepared statements, they're much safer.
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Error reporting should only be done in staging, and never production.
Also, add or die(mysqli_error($conn)) to mysqli_query().
What happen if
$num = mysqli_num_rows($result1);
Return than more than one zero row
if ($num == 0)
You code not enter into if condition and your $sql variable is empty
if (mysqli_query($conn, $sql))// here $sql is empty if $num is greater then zero
So close proper your if condition
if ($num == 0)
{
$sql = "INSERT INTO tbl_rental (game_id, first_name, last_name, address, postal_code, start_date, rental_days, borrow_game, end_date)
VALUE ('$id', '$first_name', '$last_name', '$address', '$postal_code', '$start_date', '$rental_days', '$borrow_game', '$end_date')";
Inserting the data into the table^!
if (mysqli_query($conn, $sql))
{
echo "New record created successfully";
}
else
{
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}// if condition close here
Also connection close at the end of your code
while ($row = mysqli_fetch_array($result))
{
$id = $row['game_id'];
}
mysqli_close($conn);// at last
I would appreciate it if anyone willing to tell how to echoing /print.
Below is the process of entering data into the database, before inserting it how can I echoing it to the table?
<?php
session_start();
if(isset($_POST['submit']))
{
include('class/stock_class.php');
$st = new st_exchange_conv(DEFAULT_SOURCE);
$from = mysql_real_escape_string(stripslashes($_POST['from']));
$value = floatval($_POST['amount']);
$date = date('Y-m-d H:i:s');
$_SESSION['selected'] = $from;
$stocks = $st->stocks();
asort($stocks);
foreach($stocks as $key=>$stock)
{
$st->convert($from,$key,$date);
$stc_price = $st->price($value);
$stock = mysql_real_escape_string(stripslashes($stock));
$count = "SELECT * FROM oc_stock WHERE stock = '$key'";
$result = mysql_query($count) or die(mysql_error());
$sql = '';
if(mysql_num_rows($result) == 1)
{
$sql = "UPDATE oc_stock SET stock_title = '$stock', stc_val = '$stc_price', date_updated = '$date' WHERE stock = '$key'";
}
else
{
$sql = "INSERT INTO oc_stock(stock_id,stock_title,stock,decimal_place,stc_val,date_updated) VALUES ('','$stock','$key','2',$stc_price,'$date')";
}
$result = mysql_query($sql) or die(mysql_error().'<br />'.$sql);
}
header("Location: index.php");
exit();
}
?>
Insert this:
echo "<table><tr><th>".implode(array_keys($stocks), '</th><th>')."</th></tr>";
foreach($stocks as $row) echo "<tr><td>".implode('</td><td>', $row)."</tr>";
echo "</table>";
Edit: If printing the data is the goal and the table-view is not important, I recommend print_r($stocks) instead.