table name - user_table
i try to save and update value from single Save button.
but I can not understand how to merge save and update code in 'if else' using certain condition.
Below is my save code :
<?php
require_once('includes/config.php');
$errors = array();
$Admin = new admins;
if(isset($_POST['save']))
{
$errors = $Admin->validate_cms_user_table();
if(!count($errors))
{
$table = $Admin->user_table;
$fields_func = array ('password' =>"md5('{$_POST['password']}')");
unset ($_POST['password']);
if($Admin->save_advance($table,$_POST,$fields_func))
{
$_SESSION['message'] = "User Created Successfully";
header("Location:user_master.php");
exit;
}
}
}
?>
Update code
if($Admin->save($Admin->user_table,$_POST,"id=".$_GET['id']))
{
$_SESSION['message'] = "Updated Successfully";
header("Location: user_master.php");
exit;
}
Update Condition
if(!isset($_GET['catid']))
{
//Then Save Code
}
Else
{
//Update Code
}
Related
I am new to...well everything. Bear with me.
I have a website that has a textbox for user input ( a code they receive). When they submit, it sends the code to a PHP file, which then checks the code against a database. If it matches the winner code, it redirects the user to a specific page. Losers, are redirected to a loser page.
That works!
However, I'm now trying to redirect users to a third page, if their code matches a different table of codes, but I can't figure out the if else statements. Can anyone help a poor doodle like myself? Thanks!
Here's what I got:
// Connect to your MySQL database
$dbhst = "localhost";
$dbnme = "blah";
$bdusr = "blaaah";
$dbpws = "blahblahblacksheep";
// Using PDO to connect
$conn = new PDO('mysql:host='.$dbhst.';dbname='.$dbnme, $bdusr, $dbpws);
// Getting variables
$answer = $_POST['answer'];
$questionID = $_POST['questionID'];
// Comparing answers
try {
$stmt = $conn->prepare("SELECT * FROM Winners WHERE Winners='" . $answer . "' LIMIT 0,1");
$stmt->execute();
$result = $stmt->fetchAll();
if ( count($result) ) {
foreach($result as $row) {
// echo 'Congrats, you've entered a correct code';
header("Location: https://get-a-brik.myshopify.com/pages/8522");
}
} else {
// echo 'Your code did not win. Please try again.';
header("Location: https://get-a-brik.myshopify.com/pages/5551");
exit;
}
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
?>
You can use an if ... else if ... else statement:
if (... user is winner ...) {
header("Location: https://get-a-brik.myshopify.com/pages/8522");
} else if (... user is looser ...) {
header("Location: https://get-a-brik.myshopify.com/pages/5551");
} else {
header("Location: winner page");
}
This is what I have and it's not working. I need to check if the fields in Your_Location.php is empty. If it is, throw an error. If not; run the query as follow. It would work if I throw in //if (!mysqli_query($conn,$sqlinsert))
<?php
//session_start();
include 'dbConfig.php';
include 'Your_Location.php';
$childfirst = $_POST['element_1'];
$childlast = $_POST['element_2'];
$childdobyear = $_POST['element_3_3'];
$childdobmon = $_POST['element_3_1'];
$childdobday = $_POST['element_3_2'];
$childbaptize = $_POST['element_4'];
$childrelationship = $_POST['inputrelation'];
$childdob = "$childdobyear-$childdobmon-$childdobday";
$sqlinsert="INSERT INTO memchild (ID, FirstName, LastName, DOB, Baptize, Relationship)
VALUES
('$getid2','$childfirst','$childlast','$childdob','$childbaptize','$childrelationship')";
//Build arrays of fields
$required = array('element_1', 'element_2', 'element_3_3', 'element_3_2', 'element_3_1', 'elelment_4', 'inputrelation');
//Loop to check for empties
$error = false;
foreach($required as $fields) {
if(empty($_POST[$fields])){
$error = true;
}
}
if($error){
Sleep(3)
?>
<script>
document.getElementById('li_9').innerHTML = '* $childfirst Make sure the fields are not empty.';
</script>
<?php
exit();
}Else{
mysqli_query($conn,$sqlinsert)
?>
<script>
document.getElementById('li_9').innerHTML = '$childfirst $childlast has been added.';
</script>
<?php
sleep(3);
echo "<meta http-equiv='refresh' content='0'>";
}
?>
First try to check if the keys actually exist.
$childfirst = isset($_POST['element_1'])? $_POST['element_1'] : null;
secondly, you can check what field is not filled in:
$errorMessage = '';
foreach($required as $key) {
if(empty($_POST[$key])){
$error = true;
// break; // Uncomment if you want to exit the loop if one field is not set
// $errorMessage .= $key . ' is not filled in'; // Uncomment if you want to add message for every missing key
}
}
for your scripts, you are trying to combine PHP value with javascript, you need to actually echo or print the value like this:
<script>
document.getElementById('li_9').innerHTML += "* <?php echo $childfirst;?> Make sure the fields are not empty.";
</script>
and also for the second one
<script>
document.getElementById('li_9').innerHTML += "<?php echo $childFirst . ' ' . $childLast;?> has been added.";
</script>
Hey your error is you passing string values to check and its not empty already
change this line
$required = array('element_1', 'element_2', 'element_3_3', 'element_3_2', 'element_3_1', 'elelment_4', 'inputrelation');
to
$required = array("$element_1", "$element_2");
Add the other elements too
And change this too
foreach($required as $fields) {
if(empty($fields)){
$error = true;
}
}
Hope it works for You
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";
}
i have a script that saves records to mysql with no problem. but i want to add some code to show an error message if the name or code already exists in the database. the code is like below;
<?php
$urunadi = $_POST["urunadi"];
$malzemekodu = $_POST["malzemekodu"];
$urunkategorisi = $_POST["urunkategorisi"];
$birim = $_POST["birim"];
$miktar = $_POST["miktar"];
$personel = $_POST["personel"];
$birimfiyat = $_POST["birimfiyat"];
$toplamfiyat = $_POST["birimfiyat"]*$_POST["miktar"];
$fiyatbirimi = $_POST["fiyatbirimi"];
if(empty($urunadi)){
echo("<center><b>Product name empty, please go back and fill this line.</b></center>");
}else
if(empty($miktar)){
echo("<center><b>Product amount empty, please go back and fill this line.
}else
if(empty($malzemekodu)){
echo("<center><b>Product code empty, please go back and fill this line.</b></center>");
}else
if(empty($birimfiyat)){
echo("<center><b>Unit price empty, please go back and fill this line.</b></center>");
}else{
include "database.php";
$sql = (" insert into `depo` (`urunadi`,`malzemekodu`,`urunkategorisi`,`birim`,`miktar`,`personel`,`birimfiyat`,`toplamfiyat`,`fiyatbirimi`)
VALUES ('$urunadi','$malzemekodu','$urunkategorisi','$birim','$miktar','$personel','$birimfiyat','$toplamfiyat','$fiyatbirimi');");
$kayit = mysql_query($sql);
}
if (isset ($kayit)){
echo "<center>Data saved</center>";
}
else {
echo "<center>Data could not be saved</center>";
}
if (isset($_REQUEST["kullanici"])) {
include("database.php");
$sql = ("select * from uye");
}
else {
header ("Location: uyari.html");
}
?>
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?