how to check the exisitng data from database before inserting - php

This is my code and it does not work, it's not inserting into the database. Please help me fix this problem.
$orgexist = $_POST['orgName1'];
$_SESSION['id'] = $_POST['id'];
$orgid = $_POST['id'];
$orgnme = $_POST['orgName1'];
$orgdesc = $_POST['orgDesc'];
$orgcat = $_POST['cat'];
$orgdept = $_POST['coldept'];
$orgvis = $_POST['vision'];
$orgmis = $_POST['mision'];
//get the value of category from database
//echo $orgdept;
$dept = "SELECT `col_id`, `col_description` FROM `college` WHERE `col_description` = '$orgdept'";
$deptresult = mysql_query($dept);
while ($rows = mysql_fetch_array($deptresult)) {
$getcol = $rows['col_id'];
//echo $getcol;
}
$sqlorg = mysql_query("SELECT * FROM `organization`");
while ($orgrows = mysql_fetch_array($sqlorg)) {
//$dborgid = $orgrows['org_id'];
$dborgnme = $orgrows['org_name'];
}
if ($dborgnme == $orgexist) {
echo "<script type='text/javascript'>
alert('Organization Name Already Used by other Organization');
history.back();
</script>";
} else {
$orginsrt = mysql_query("INSERT INTO `organization`(`org_id`,`org_name`,`org_desc`,`category`,`vision`,`mission`,`col_id`,`image`) VALUES ('$orgid','$orgexist','$orgdesc','$orgcat','$orgvis','$orgmis','$getcol','$image')");
echo "<script type='text/javascript'>
alert('Proceed to next Step');</script>";
//require ('orgsignup.php');
header('Location:orgsignup2.php');
//echo "Not in the Record";
}
}

You're using deprected functions, replace mysql_query by mysqli_query.
For more references see http://php.net/manual/en/function.mysql-query.php

Related

alert shows entry updated succesfully but entry does'nt update in database

I am creating a updating form page. it shows no error but there is no updation in my database.
I already check whole the database row names but that doesnt help.
that code is a little bit long but please help.
<?php
if (isset($_POST['update'])) {
$update_id = $_GET['edit_form'];
$bill = $_POST['b_no'];
$naam = $_POST['name'];
$mobile_no = $_POST['mobile'];
$addres = $_POST['add'];
$detail = $_POST['p_detail'];
$p_img_name = $_FILES['p_img']['name'];
$p_img_type = $_FILES['p_img']['type'];
$p_img_size = $_FILES['p_img']['size'];
$p_img_tmp = $_FILES['p_img']['tmp_name'];
$prc = $_POST['price'];
$deposite = $_POST['d_amt'];
$remaning = $_POST['r_amt'];
$b_img_name = $_FILES['b_img']['name'];
$b_img_type = $_FILES['b_img']['type'];
$b_img_size = $_FILES['b_img']['size'];
$b_img_tmp = $_FILES['b_img']['tmp_name'];
$p_date = date('y-m-d');
move_uploaded_file($p_img_tmp, "images/Product/$p_img_name");
move_uploaded_file($b_img_tmp, "images/Bill/$b_img_name");
$update_query = "UPDATE new_entry SET bill_no='$bill', name='$naam',
mobile_no='$mobile_no', address='$addres', product_detail='detail',
product_image='$p_img_name', price='$prc', deposite_amt='deposite',
remaining='$remaning', bill_image='$b_img_name',Product_date='$p_date'
WHERE s_no='$update_id' ";
if(mysqli_query($conn, $update_query)) {
echo "<script> alert('Entry Updated Successfully') </script>";
echo "<script> window.open('view_entry.php','_self') </script>";
} else {
echo "cant Update Entry.." .mysqli_error($conn);
}
}
?>

Not able to insert the data from url to database in php

final.php
Here I am trying to get the data from the url using GET method and trying to insert into the database. I was able to insert the data for first few rows after that the data is not inserted. Can anyone help me regarding this?
when I try to run the url: www.myii.com/app/final.php?name=123&glucose=3232...
the data is not inserting.
<?php
include("query_connect.php");
$name = $_GET['name'];
$glucose = $_GET['glucose'];
$temp = $_GET['temp'];
$battery = $_GET['battery'];
$tgs_a = $_GET['tgs_a'];
$tgs_g = $_GET['tgs_g'];
$heartrate = $_GET['heartrate'];
$spo2 = $_GET['spo2'];
$rr = $_GET['rr'];
$hb = $_GET['hb'];
$ina22 = $_GET['ina22'];
$accucheck = $_GET['accucheck'];
$isactive = $_GET['isactive'];
$address = $_GET['address'];
$deviceno = $_GET['deviceno'];
$sql_insert = "insert into query (name,glucose,temp,battery,tgs_a,tgs_g,heartrate,spo2,rr,hb,ina22,accucheck,isactive,address,deviceno) values ('$name','$glucose','$temp',$battery','$tgs_a','$tgs_g','$heartrate','$spo2','$rr','$hb','$ina22','$accucheck','$isactive','$address','$deviceno')";
mysqli_query($sql_insert);
if($sql_insert)
{
echo "Saving succeed";
//echo $date_time;
}
else{
echo "Error occured";
}
?>
Query_connect.php
This is my database config php file.
<?php
$user = "m33root";
$password = "me3i434";
$host = "localhost";
$connection = mysqli_connect($host,$user,$password);
$select = mysqli_select_db('miiyy',$connection);
if($connection)
{
echo "connection succesfull<br>";
}
else {
echo "Error";
}
?>
Make sure that all columns can contain NULL so that not filled fields will stay NULL instead of throwing an error.
Try below to see mysql error:
mysql_query($sql_insert);
echo mysql_error()
Try these
In SQL
Change the table name of query to some other name. Because "query" is reserved in SQL
In code
if (!mysqli_query($con,$sql_insert ))
{
echo("Error description: " . mysqli_error($con));
}
else
{
echo "Success";
}
Use mysqli_error() function

Check if row in table is 'equal' to other row

I have the following code to check if a row exists in MySQL:
<?php
if (!empty($_POST)) {
$code = $_POST['code'];
mysql_connect("$dbhost","$dbuser","$dbpass");
mysql_select_db("$dbname");
$result = mysql_query("SELECT 1 FROM files WHERE id='$code' LIMIT 1");
if (mysql_fetch_row($result)) {
echo 'Exists';
} else {
echo 'Does not exist';
}
}
?>
This works fine. But I need to change it a bit. I have the following fields:
id, title, url, type. When someone uses the code above ^ to check if a row exists, I need a variable to get the url from the same row, so I can redirect the user to there.
Do you have any idea how I can do that?
Thanks in advance! :)
Try this:
<?php
if (!empty($_POST)) {
$code = $_POST['code'];
mysql_connect("$dbhost","$dbuser","$dbpass");
mysql_select_db("$dbname");
$result = mysql_query("SELECT * FROM files WHERE id=" . $code . " LIMIT 1");
if (mysql_num_rows($result) > 0) {
while($rows = mysql_fetch_array($result)) {
echo 'Exists';
$url = $rows['url'];
}
} else {
echo 'Does not exist';
}
}
?>
It is quite simple. I think you don't show any effort to find the solution by yourself.
<?php
if (!empty($_POST)) {
$code = $_POST['code'];
mysql_connect("$dbhost","$dbuser","$dbpass");
mysql_select_db("$dbname");
$result = mysql_query("SELECT url FROM files WHERE id='$code' LIMIT 1");
if ($result) {
$url = mysql_fetch_row($resultado);
} else {
echo 'Does not exist';
}
}
<?php
$sql_query = "SELECT * FROM test WHERE userid ='$userid'";
$result1 =mysql_query($sql_query);
if(mysql_num_rows($result1)>0){
while($post = mysql_fetch_array($result1))
{
$url = $post['url'];
}
}
?>
If mysql_num_rows($result1)>0 it means row is existed fir the given user id

query wongt output to table. correct syntax

What am i doing wrong here?
(Know its mysql)
this else is not working it look like. user can insert same modul id many times.
$besvarelse = $_GET['besvarelse'];
$modulid = $_GET['modulid'];
$username = $_GET['username'];
$tilkobling = kobleTil();
if (!empty($besvarelse) ) {
$insert = mysql_query("INSERT INTO oppgave (besvarelse, modulid, username) VALUES ('$besvarelse', '$modulid','$username')");
if($insert) {
echo "<h1>Levering OK</h1><br>";
}
}
else {
die("<h1>Du har lever før</h1>");
}
?>
Try with this and post the errors that PHP shows, please:
//Assuming that connection to mysql server its done somewhere with mysql_connect()
error_reporting(E_ALL); ini_set('display_errors', 1);
explode($_GET);
echo "<pre>";
print_r($_GET); //Just to see what's on the variables...
echo "</pre>;
// $tilkobling = kobleTil(); Need to explain this line
if (!empty($besvarelse) ) {
$sql = "INSERT INTO oppgave (besvarelse, modulid, username) VALUES ('$besvarelse', '$modulid','$username')";
if($insert = mysql_query($sql))
{
echo "<h1>Levering OK</h1><br>";
}
else {
die("<h1>Du har lever før</h1>");
}
} // you were missmatching the IF closing bracket
?>
try to check for exist rows like
$besvarelse = $_GET['besvarelse'];
$modulid = $_GET['modulid'];
$username = $_GET['username'];
$tilkobling = kobleTil();
if (!empty($besvarelse) ) {
// check exist moduleid
$check = mysql_query("select * from oppgave where modulid = '$modulid'");
$conut_rows= mysql_num_rows($check);
if($conut_rows > 0) {
echo "module id already exist";
}
else {
$insert = mysql_query("INSERT INTO oppgave (besvarelse, modulid, username) VALUES ('$besvarelse', '$modulid','$username')");
if($insert) {
echo "<h1>Levering OK</h1><br>";
}
else {
die("<h1>Du har lever før</h1>");
}
}
}

passing and receiving string variables using php

I am passing the string value through link in the URL to the next page like this <a href="ApplicationRegister.php?plan=trial">
In the ApplicationRegister.php page, i am getting this value like this $plan = $_GET["plan"];
and i will put this into a session variable like this $_SESSION['plans'] = $plan;
Here i am getting the value. but after the if statement i am not getting the value for this plan even after using Session variable.
My complete code is like this
$plan = $_GET["plan"];
echo $plan;
$_SESSION['plan'] = $plan;
$plans = $_SESSION['plan'];
echo $_SESSION['plans'];
include('connect.php');
If (isset($_POST['submit']))
{
$CompanyName = $_POST['CompanyName'];
$CompanyEmail = $_POST['CompanyEmail'];
$CompanyContact = $_POST['CompanyContact'];
$CompanyAddress = $_POST['CompanyAddress'];
$StoreName = $_POST['StoreName'];
echo $plans;
$myURL ="$_SERVER[HTTP_HOST]";
$myURL =$StoreName.'.'.$myURL;
if (stripos($myURL, 'www.') !== 0) {
$myURL = 'www.' . $myURL;
}
if (stripos($myURL, 'http://') !== 0) {
$myURL = 'http://' .$myURL;
}
if(stripos($myURL, '.com') !== 0) {
$myURL = $myURL . '.com';
}
echo $plans;
$RegistrationType = $_POST['RegistrationType'];
$Status = "Active";
$sql = "select * from plans where planname = '$plans'";
echo $sql;
mysql_query($sql) or die (mysql_error());
$planID = $row['planid'];
$query1 = "select count(CompanyEmail) from ApplicationRegister where CompanyEmail = '$CompanyEmail'" ;
$result1 = mysql_query($query1) or die ("ERROR: " . mysql_error());
$msg = "";
while ($row = mysql_fetch_array($result1))
{
if($row['count(CompanyEmail)'] > 0)
{
$msg = "<font color='red'> <b>This E-mail id is already registered </b></font> ";
break;
}
}
if($msg == "")
{
$query2 = "select count(URL) from ApplicationRegister where URL = '$myURL' ";
$result2 = mysql_query($query2) or die ("ERROR: " . mysql_error());
$msg = "";
while ($row = mysql_fetch_array($result2))
{
if($row['count(URL)'] > 0)
{
$msg = "<font color='red'> <b>This Stroename is already registered </b></font> ";
break;
}
}
if($msg == "")
{
$sql = "INSERT INTO ApplicationRegister(planid, CompanyName, CompanyEmail, CompanyContact, CompanyAddress, RegistrationType, ApplicationPlan, ApplicationStatus, URL, CreatedDate) VALUES ('$planID', '$CompanyName', '$CompanyEmail', '$CompanyContact', '$CompanyAddress', '$RegistrationType', '$plans', '$Status', '$myURL', NOW() )";
mysql_query($sql) or die(mysql_error());
$id = mysql_insert_id();
$_SESSION['application_id'] = $id;
if($plans == "trail")
{
header("Location: userRegister.php");
exit();
}
else
{
header("Location : PaymentGateway.php");
exit();
}
}
}
}
?>
Only in the beginning it holds the value , if i try to display it within theIf (isset($_POST['submit'])) it shows blank value for plans. Do not know what to do. Plz suggest
EDITED
Even after using like this, its the same. i do not know what may be the problem :(
$plan = $_GET["plan"];
echo $plan;
$_SESSION['plans'] = $plans;
echo $_SESSION['plans'];
// $plan = +$plan;
include('connect.php');
If (isset($_POST['submit']))
{
$CompanyName = $_POST['CompanyName'];
$CompanyEmail = $_POST['CompanyEmail'];
$CompanyContact = $_POST['CompanyContact'];
$CompanyAddress = $_POST['CompanyAddress'];
$StoreName = $_POST['StoreName'];
echo $_SESSION['plans'];
EDITED
In ApplicationRegister.php, i have passed the hiddenvalue which i got fro\m previous page like this
<input type="hidden" name="plan" value="<?php echo $plan ?>"/>
then POST method i have used this. Now i am getting the value for it. Thanks to all
EDITED
if($PlanName == "trail")
{
header("Location: userRegister.php");
exit();
}
else
{
header("Location : PaymentGateway.php");
exit();
}
It's because you're not calling session_start() at the top of the page. You need that for your sessions to persist across requests (which is the point of sessions)
As well as not calling session_start();, this code is wrong:
$plan = $_GET["plan"];
echo $plan;
$_SESSION['plan'] = $plan;
$plans = $_SESSION['plan'];
echo $_SESSION['plans'];
It should be:
$plan = $_GET["plan"];
echo $plan;
$_SESSION['plan'] = $plan;
$plans = $_SESSION['plans'];
echo $_SESSION['plans'];
You are setting $_SESSION['plan'] and then trying to access $_SESSION['plans'].
Also, are you clicking a link or submitting a form? You say that you have a link, yet your code tries to access values passed from a form.
If you are using a form, don't use links. Instead, use a select element to select a plan, and then change $plan = $_GET["plan"]; to $plan = $_POST["plan"];.
EDIT:
For the redirection problem, try this code:
echo "<pre>** Plan Name: **\n";
var_dump($PlanName);
echo "</pre>";
if($PlanName == "trail")
{
header("Location: userRegister.php");
exit();
}
else
{
header("Location: PaymentGateway.php");
exit();
}
and see what it outputs.
When someone clicks the link, it's going to set the variable properly. However, it's not going to hit the $_POST['submit'] logic, because it's not a post, just a get. Then, assuming your actually posting to that page at a later point, trying to access anything in $_GET will be null, and will then reset the session variable to null.
Your first page should have code something like this
<form action="ApplicationRegister.php" method="post">
<select name="plan">
<option value="trial">Trial</option>
</select>
<input type="submit"/>
</form>
Then, you check for $_POST['plan'] and $_POST['submit']

Categories