I'm new to web development and managed to create a form which collects data but discovered that there are some duplicate values that manage to enter the database on submit. Any ideas on how to maintain that data is captured only once into the database? Below is my code that posts details of form on submit.
<?php include 'database.php';?>
<?php
$customer_name = data_input($_POST["customer_name"]);
$customer_gender = data_input($_POST["customer_gender"]);
$mobile_number = data_input($_POST["mobile_number"]);
$unique_box_id = data_input($_POST["unique_box_id"]);
$casn_number = data_input($_POST["casn_number"]);
$customer_address = data_input($_POST["customer_address"]);
$dso_region = data_input($_POST["dso_regions"]);
$state = data_input($_POST["state"]);
$decoder_type = data_input($_POST["decoder_type"]);
$antennae_type = data_input($_POST["antennae_type"]);
$brand_of_box = data_input($_POST["brand_of_box"]);
$call_category = data_input($_POST["call_category"]);
$complaint_category = data_input($_POST["complaint_category"]);
$agent_notes = data_input($_POST["agent_notes"]);
$resolution = data_input($_POST["resolution"]);
$agent_name = data_input($_POST["agent_name"]);
function data_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
mysqli_query($connect,"INSERT INTO customer_details (NAMES, GENDER,MOBILE_NUMBER,UNIQUE_BOX_ID,CASN_NUMBER,
CUSTOMER_ADDRESS,DSO_REGION,STATE,DECODER_TYPE,ANTENNAE_TYPE,BRAND_OF_BOX,
CALL_CATEGORY,COMPLAINT_CATEGORY,AGENT_NOTES,RESOLUTION,AGENT_NAME)
VALUES ('$customer_name', '$customer_gender', '$mobile_number','$unique_box_id','$casn_number','$customer_address','$dso_region','$state','$decoder_type',
'$antennae_type','$brand_of_box','$call_category','$complaint_category','$agent_notes','$resolution','$agent_name')");
if (mysqli_affected_rows($connect)> 0){
echo "<p>Customer Details submitted</p>";
//echo "Go Back";
header("Location: {$_SERVER['HTTP_REFERER']}");
exit;
} else {
echo "Customer Details NOT submitted<br />";
echo mysqli_error ($connect);
}
mysql_close($connnet)
?>
I think your if condition is wrong, you are checking on connection variable
But you should check another variable, like this
$insertQuert = mysqli_query($connect,"INSERT INTO ...
then in if condition
if (mysqli_affected_rows($insertQuery)> 0){
Related
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);
}
}
?>
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
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
I'm a noob trying to get some functionalities done on a website. Unfortunately, I have very little authorization, so I would like to create a comment box as simply as possible without the use of databases or jQuery, JavaScript.
I did a lot of searches and I believe the easiest way is to create a log-like HTML with a comment form, to which the PHP script would append the entered text. This is what I've managed to fabricate so far:
$file = "updates.html";
$fh = fopen($file, 'a');
$file = "updates.html";
$fh = fopen($file, 'a');
$comment = echo $_POST["update"] \n";
fwrite($fh, $comment);
fclose($fh);
The updates.html file has a comment box of which action points at a php file with the content above. Of course, it doesn't work, there is a parsing error, but I have no idea how to use the variable there (if that's the cause of the problem). I just can't figure out how to do it... Do you have any suggestions? Thanks!
don't know what you would like to do ....
<?php
if(isset($_POST['update'])) {
// if a request update exists
$file = "updates.html";
file_put_contents($file, $_POST['update']."\n",FILE_APPEND);
}
?>
You opened your file twice.
You don't need to echo your $_POST['update]
<?php
$file = "updates.html";
$fh = fopen($file, 'a');
$comment = $_POST["update"] . "\n";
fwrite($fh, $comment);
fclose($fh);
?>
This answer is for anyone who may have a similar question in future.
In as much as adding comment without a database is impractical it is doable. Below is how you can go about it.
Step 1: Create a file and save it with the .php extension like comment.php
Step 2. create the usual html form and set the form method = "post" and form action to the name of the file like action = "comment.php"
<h3> Add a comment here </h3>
<form action="comment.php" method="post">
<label for="name">Name:</label>
<input type="text" name="yourname"><br>
<label for="name">Comment:</label>
<textarea name="comment" id="comment" cols="30" rows="10"></textarea>
<input type="submit" value="submit">
</form>
`
Step 3.
Write a php script within the same file comment.php to process the data from the form. Remember to enclose the script in the php tag
<?php
$yourname = $_POST['yourname'];
$comment = $_POST['comment'];
// format the comment data into how you want it to be displayed on the page
$data = $yourname . "<br>" . $comment . "<br><br>";
//Open a text file for writing and save it in a variable of your chosen.
//Remember to use "a" not "w" to indicate write. Using 'w' will overwrite
// any existing item in the file whenever a new item is written to it.
$myfile = fopen("comment.txt", "a");
//write the formatted data into the opened file and close it
fwrite($myfile, $data);
fclose($myfile);
// Reopen the file for reading, echo the content and close the file
$myfile = fopen("comment.txt", "r");
echo fread($myfile,filesize("comment.txt"));
?>
Puting it in file may cause problem because you need some kind of delimiter, you could encode it with base64 and append \n in the end of an entry
$input = base64_encode(htmlspecialchars($_POST['update'])); //consider using strip_tags as well to avoid injections
file_put_contents("updates.html", $input."\n");
to get entries use
$entires = file("updates.html");
if(count($entries) > 0)
{
foreach($entries as $entry)
{
echo base64_decode($entry);
}
}
else
{
echo 'no entries so far';
}
You should consider using at least SimpleXml if you don't want to use Db.
Required Field
"; } else { $v_firstname = ""; } if ($lastname=="") { $v_lastname= "Required Field
"; } else { $v_lastname= ""; } if ($password=="") { $v_password= "Required Field
"; } else { $v_password= ""; } if ($password!=$passwordRetype) { $v_passwordRetype= "Password did not match!
"; } else { $v_passwordRetype= ""; } if ($gender=="") { $v_gender= "Required Field
"; } else { $v_gender= ""; } if ($student_id=="") { $v_student_id= "Required Field
"; } else { $v_student_id= ""; } if ($firstname!="" && $lastname!= "" && $password == $passwordRetype && $student_id!= "" && $email!= "" && $gender!= ""){ $checkme=mysql_query("SELECT * FROM members WHERE student_id = '$student_id'") or die(mysql_error()); $checkmyid=mysql_numrows($checkme); if($checkmyid > 0){ header("location:checkid.php"); }else{ mysql_query("INSERT INTO members (firstname, lastname, password,url, gender, student_id, status_id,photo,account_status) VALUES ('$firstname','$lastname','$password','$email','$gender','$student_id','0','default.jpg','0')")or die(mysql_error()); $wewness = mysql_query("SELECT * FROM members WHERE student_id = $student_id")or die(mysql_error()); $getid = mysql_fetch_array($wewness); $_SESSION['member_id'] = $getid['memberid']; $_SESSION['login'] = 'true'; $_SESSION['studentid'] = $student_id; header("location:registerexec.php"); } } } if(isset($_POST['login'])){ $studentid = $_POST['studid']; $pass = $_POST['password']; $query2 = mysql_query("SELECT * FROM members WHERE student_id = '$studentid' AND password = '$pass' ") or die (mysql_error()); while($studid = mysql_fetch_object($query2)) { echo "$studid->member_id"; } $numberOfRows = MYSQL_NUMROWS($query2); if ($numberOfRows == 0) { } else if ($numberOfRows > 0){ $wewness = mysql_query("SELECT * FROM members WHERE student_id = $studentid")or die(mysql_error()); $getid = mysql_fetch_array($wewness); if($getid['account_status']==0){ $_SESSION['login'] = 'maybe'; $_SESSION['member_id'] = $getid['member_id']; $_SESSION['studentid'] = $getid['student_id']; header('location:registerexec.php'); }elseif($getid['account_status']==2){ $_SESSION['login'] = 'true'; $_SESSION['member_id'] = $getid['member_id']; $_SESSION['studentid'] = $getid['student_id']; header('location:hometest.php'); }elseif($getid['account_status']==1){ $_SESSION['login'] = 'maybe'; $_SESSION['member_id'] = $getid['member_id']; $_SESSION['studentid'] = $getid['student_id']; header('location:fill.php'); } } } ?>
by glen
I am using a single page submission form to create and also edit a record. It creates the record fine and will populate the form and update it if I define the record id myself.
It will not update if I try and pull in the $_GET value.
Here is what I have:
if(isset($_GET['id'])) {
$id = $_GET['id'];
$guests = Guest::find_by_id($id);
}
$message = "";
if(isset($_POST['submit'])) {
$guest = new Guest();
if (isset($_GET['id'])) {
$guest->id = $id;
}
$guest->name = $_POST['guest_name'];
$guest->info = $_POST['guest_info'];
$guest->image_path = $_POST['guest_image_path'];
$guest->sc_player = $_POST['guest_sc_player'];
if($guest->update()) {
$message = "Uploaded successfully.";
} else {
$message = join("<br />", $guest->errors);
}
}
It works if I replace the second if statement with $guest->id = "14"
Where is your ID I can't see it anywhere, where is it set?