Related
This is my code my $sql variable didn't give query plese help me for this I try this but I couldn't please help me with that
<?php
$connect = mysqli_connect("localhost", "root", "", "finger");
$f= "";
$l= "";
$sql = "CREATE TRIGGER `ersdmmmmecv` AFTER INSERT ON `event` FOR EACH ROW SELECT fname,Lname INTO $f,$l FROM user WHERE id=NEW.id;"
$result = mysqli_query($connect, $sql);
?>
You cannot use a MySQL trigger to update PHP variables. If you want the values of $f and $l to update whenever a new record is inserted into your event table, you need to do this in PHP entirely.
Something along these lines should work (note: I did not test this myself):
$f = "";
$l = "";
$new_id = "id_value";
$insert = $connect->prepare("INSERT INTO `event` (`id`, `column2`, `column3`) VALUES (?, ?, ?)");
$insert->bind_param("sss", $new_id, "value2", "value3");
if ($insert->execute() === FALSE) {
echo 'Could not insert event: ' . $insert->error;
} else {
// If `event`.`id` is actually an AUTO_INCREMENT column, and you don't
// specify it in your INSERT query, use this here:
// $new_id = $insert->insert_id;
$select = $connect->prepare("SELECT `fname`, `Lname` FROM `user` WHERE `id` = ?");
$select->bind_param("s", $new_id);
$select->execute();
$select->bind_result($f, $l);
$success = $select->fetch();
if ($success !== TRUE) {
echo 'Could not update $f and $l with new values: '
. ($select->error ?: 'No user with id: ' . $new_id);
}
}
If you have multiple places in your code where you insert data into the events table, I would personally wrap this in a function so I wouldn't have to repeat this every time.
This is the Trigger Solution
<?php
$connect = mysqli_connect("localhost", "root", "", "finger");
$sql1 = "CREATE TRIGGER `ersdmmmmecv` AFTER INSERT ON `event` FOR EACH ROW INSERT INTO res (fres,lres) VALUES SELECT fname,Lname FROM user WHERE id=NEW.id;";
$result2 = mysqli_query($connect, $sql1);
$sql = "SELECT * FROM res;";
if( !( $selectRes = mysqli_query($connect, $sql) ) ){
echo 'Retrieval of data from Database Failed - #';
}else{
?>
<table border="2">
<thead>
<tr>
<th>fName</th>
<th>lname</th>
</tr>
</thead>
<tbody>
<?php
if( mysqli_num_rows( $selectRes )==0 ){
$print_output= '<tr><td colspan="4">No Rows Returned</td></tr>';
}else{
while( $row = mysqli_fetch_assoc( $selectRes ) ){
$print_output="<tr><td>{$row['fres']}</td><td>{$row['lres']}</td></tr>\n";
}
}
?>
</tbody>
</table>
<?php
try
{
$fp=pfsockopen("127.0.0.1", 80);
fputs($fp, $print_output);
fclose($fp);
echo 'Successfully Printed '.$print_output;
}
catch (Exception $e)
{
echo 'Caught exception: ', $e->getMessage(), "\n";
}
?>
<?php
}
?>
<?php
$sql2= "DROP TRIGGER ersdmmmmecv";
$result1 = mysqli_query($connect, $sql2);
$sql3= "DELETE FROM res;";
$result3 = mysqli_query($connect, $sql3);
?>
<script>
setTimeout(function () { window.location.reload(); }, 1*60*1000);
// just show current time stamp to see time of last refresh.
document.write(new Date());
</script>
so i moved all the data from mysql control center(outdated) to workbench
all the connections seems to be successful but i cant retrieve any data while loggin in.its blank page..was working before i migrated everything over
see the code and screenshot below..any help would be appreciated
enter image description here
include('dbconnect.php');
if(isset($_POST['submit']))
{
$error_username = '';
$error_password = '';
$error_general = '';
$usernamedd = $_POST['username'];
$passworddd = $_POST['password'];
if($usernamedd == '')
$error_username = '<font color="red">- No username entered</font>';
if($passworddd == '')
$error_password = '<font color="red">- No password entered</font>';
if($usernamedd != '' && $passworddd != '')
{
//echo "SELECT username,firstname,lastname,perm_calendar,perm_users,perm_market,perm_newsletter FROM emp_tbl WHERE username='$usernamedd' AND password='$passworddd'";
$result = odbc_exec($conn, "SELECT username,firstname,lastname,perm_calendar,perm_users,perm_market,perm_newsletter,perm_topagents FROM emp_tbl WHERE (username='$usernamedd' OR email='$usernamedd') AND MD5(password)=MD5('$passworddd') LIMIT 1");
//echo "SELECT username,firstname,lastname,perm_calendar,perm_users,perm_market,perm_newsletter FROM emp_tbl WHERE username='$usernamedd' AND MD5(password)=MD5('$passworddd') LIMIT 1";
//echo "SELECT username FROM staff WHERE username='$username' AND dept='Admin'<br><br>";
$_username = odbc_result($result, "username");
$_fullname = odbc_result($result, "firstname").' '.substr(odbc_result($result, "lastname"),0,1).'.';
$_perm_calendar = odbc_result($result, "perm_calendar");
$_perm_users = odbc_result($result, "perm_users");
$_perm_market = odbc_result($result, "perm_market");
$_perm_newsletter = odbc_result($result, "perm_newsletter");
$_perm_topagents = odbc_result($result, "perm_topagents");
//echo "<br><br>username: ".$_username;
//echo "<br>full name: ".$_fullname;
//echo "<br>perm_calendar: ".$_perm_calendar;
//echo "<br>per_users: ".$_perm_users;
//echo "<br>perm_market: ".$_perm_market;
//echo "<br>perm_newsletter: ".$_perm_newsletter;
if($_username != '')
{
if(isset($_POST['save']))
{
setcookie("username",odbc_result($result, 'username'),time()+604000);
$resultlog = odbc_exec($conn, "INSERT INTO logs_login (date, file, username, pwd, description, type, ip) VALUES (NOW(), '$file', '$usernamedd', '$passworddd', 'Success : 7 days', 'login', '$ip')");
}
else
{
setcookie("username",odbc_result($result, 'username'));
$resultlog = odbc_exec($conn, "INSERT INTO logs_login (date, file, username, pwd, description, type, ip) VALUES (NOW(), '$file', '$usernamedd', '$passworddd', 'Success : Browser Session', 'login', '$ip')");
}
$_username=odbc_result($result, 'username');
$logged_in = 1;
header("Location: admin_directory.php");
}
else
{
$logged_in = 0;
$error_general = '<font color="red">- No such user or invalid password</font>';
$resultlog = odbc_exec($conn, "INSERT INTO logs_login (date, file, username, pwd, description, type, ip) VALUES (NOW(), '$file', '$usernamedd', '$passworddd', 'Failure: Incorrect user/pwd', 'login', '$ip')");
}
}
}
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
I am using php mysql pdo in here and trying to concatenate fname and lname but nothing going right am encountering {"error":true,"error_msg":"Unknown error occurred in registration!"} ..plzz help me out,pardon me if am wrong
.php
<?php
/*
starts with database connection
and gives out the result of query
in json format
*/
require_once 'DB_Functions.php';
$db = new DB_Functions();
// json response array
$response = array("error" => false);
//proceed if fields are not empty
if (!empty($_POST['salutation']) && !empty($_POST['fname']) && !empty($_POST['mname']) && !empty($_POST['lname']) && !empty($_POST['pob']) && !empty($_POST['dob']) && !empty($_POST['qualification']) && !empty($_POST['pg']) && !empty($_POST['pgy']) && !empty($_POST['graduation']) && !empty($_POST['gy']) && !empty($_POST['schooling']) && !empty($_POST['sy']) && !empty($_POST['religion']) && !empty($_POST['caste']) && !empty($_POST['subcaste']) && !empty($_POST['familyname']) && !empty($_POST['fathername']) && !empty($_POST['mothername']) && !empty($_POST['brothers']) && !empty($_POST['sisters'])){
//reciving the post parameters
$salutation =$_POST['salutation'];
$fname = trim($_POST['fname']);
$mname = trim($_POST['mname']);
$lname = trim($_POST['lname']);
$pob = trim($_POST['pob']);
$dob = trim($_POST['dob']);
$qualification = trim($_POST['qualification']);
$pg = trim($_POST['pg']);
$pgy = trim($_POST['pgy']);
$graduation = trim($_POST['graduation']);
$gy = trim($_POST['gy']);
$schooling = trim($_POST['schooling']);
$sy = trim($_POST['sy']);
$religion = trim($_POST['religion']);
$caste = trim($_POST['caste']);
$subcaste = trim($_POST['subcaste']);
$familyname = trim($_POST['familyname']);
$fathername = trim($_POST['fathername']);
$mothername = trim($_POST['mothername']);
$brothers = trim($_POST['brothers']);
$sisters = trim($_POST['sisters']);
/*
validation process
begins from here
*/
// create a new user profile
$user = $db->storeUserProfile($salutation, $fname, $mname, $lname, $pob, $dob, $qualification, $pg, $pgy, $graduation, $gy, $schooling, $sy, $religion, $caste, $subcaste, $familyname, $fathername, $mothername, $brothers, $sisters);
if ($user){
// user stored successfully as post params passed
$response["error"] = false;
$response["uid"] = $user["id"];
$response["user"]["salutation"] = $user["salutation"];
$response["user"]["fname"] = $user["fname"];
$response["user"]["mname"] = $user["mname"];
$response["user"]["lname"] = $user["lname"];
$response["user"]["pob"] = $user["pob"];
$response["user"]["dob"] = $user["dob"];
$response["user"]["qualification"] = $user["qualification"];
$response["user"]["pg"] = $user["pg"];
$response["user"]["pgy"] = $user["pgy"];
$response["user"]["graduation"] = $user["graduation"];
$response["user"]["gy"] = $user["gy"];
$response["user"]["schooling"] = $user["schooling"];
$response["user"]["sy"] = $user["sy"];
$response["user"]["religion"] = $user["religion"];
$response["user"]["caste"] = $user["caste"];
$response["user"]["subcaste"] = $user["subcaste"];
$response["user"]["familyname"] = $user["familyname"];
$response["user"]["fathername"] = $user["fathername"];
$response["user"]["mothername"] = $user["mothername"];
$response["user"]["brothers"] = $user["brothers"];
$response["user"]["sisters"] = $user["sisters"];
$response["user"]["uuid"] = $user["unique_id"];
$response["user"]["created_at"] = $user["created_at"];
$response["user"]["updated_at"] = $user["updated_at"];
echo json_encode($response);
} else {
// user failed to store
$response["error"] = true;
$response["error_msg"] = "Unknown error occurred in registration!";
echo json_encode($response);
}
}else{
//missing the required fields
$response["error"] = true;
$response["error_msg"] = "Please fill all the required parameters!";
echo json_encode($response);
}
?>
this is the database part using pdo.
php
public function storeUserProfile($salutation, $fname, $mname, $lname, $pob, $dob, $qualification, $pg, $pgy, $graduation, $gy, $schooling, $sy, $religion, $caste, $subcaste, $familyname, $fathername, $mothername, $brothers, $sisters){
try {
$characters = '0123456789';
$uuid = '';
$random_string_length = 6;
for ($i = 0; $i < $random_string_length; $i++) {
$uuid .= $characters[rand(0, strlen($characters) - 1)];
}
$sql = "INSERT INTO profile_info(salutation, fname, mname, lname, fullname, pob, dob, qualification, pg, pgy, graduation, gy, schooling, sy, religion, caste, subcaste, familyname, fathername, mothername, brothers, sisters, unique_id, created_at) VALUES ( '$salutation', '$fname', '$mname', '$lname', '$fname'.', '.'$lname', '$pob', '$dob', '$qualification', '$pg', '$pgy', '$graduation', '$gy', '$schooling', '$sy', '$religion', '$caste', '$subcaste', '$familyname', '$fathername', '$mothername', '$brothers', '$sisters', '$uuid', NOW())";
$dbh = $this->db->prepare($sql);
if($dbh->execute()){
//concatenate the strings
$sql = "UPDATE profile_info SET fullname = CONCAT(fname, ', ', lname)";
$dbh = $this->db->prepare($sql);
$dbh->execute();
// get user details
$sql = "SELECT * FROM profile_info WHERE familyname = '$familyname' LIMIT 1";
$dbh = $this->db->prepare($sql);
$result = $dbh->execute();
$rows = $dbh->fetch();
$n = count($rows);
if($n){
return $rows;
}
}
}
catch (Exception $e) {
die('Error accessing database: ' . $e->getMessage());
}
return false;
}
The concatenation of first name and last name in your INSERT query is incorrect. Use a $fullname variable to specify full name of the person, and use that variable in your INSERT query. That way you won't have to update the row because you have already inserted the row with the correct full name.
Your code should be like this:
// your code
$fullname = $fname . ", " . $lname;
$sql = "INSERT INTO profile_info(salutation, fname, mname, lname, fullname, pob, dob, qualification, pg, pgy, graduation, gy, schooling, sy, religion, caste, subcaste, familyname, fathername, mothername, brothers, sisters, unique_id, created_at) VALUES ( '$salutation', '$fname', '$mname', '$lname', '$fullname', '$pob', '$dob', '$qualification', '$pg', '$pgy', '$graduation', '$gy', '$schooling', '$sy', '$religion', '$caste', '$subcaste', '$familyname', '$fathername', '$mothername', '$brothers', '$sisters', '$uuid', NOW())";
$dbh = $this->db->prepare($sql);
if($dbh->execute()){
// get user details
$sql = "SELECT * FROM profile_info WHERE familyname = '$familyname' LIMIT 1";
$dbh = $this->db->prepare($sql);
$result = $dbh->execute();
$rows = $dbh->fetch();
$n = count($rows);
if($n){
return $rows;
}
}
// your code
If I understand the issue properly, the values are not being inserted because you are executing, instead, a SELECT statement. SELECT statements do not modify table data. You would instead do something like this:
UPDATE profile_info SET fullname = CONCAT(fname, ', ', lname);
Note, this would update the entire table....
This will fill in a pre-existing column with the new concatenated value made from the fname and lname values of each row.
Of course, if your table does not currently have a column for fullname, add one:
ALTER TABLE profile_info ADD COLUMN fullname varchar(25);
UPDATE
Take this line out:
$sql = UPDATE profile_info SET fullname = CONCAT(fname, ', ', lname);
And change this line:
$sql = "INSERT INTO profile_info(salutation, fname, mname, lname, fullname, pob, dob, qualification, pg, pgy, graduation, gy, schooling, sy, religion, caste, subcaste, familyname, fathername, mothername, brothers, sisters, unique_id, created_at) VALUES ( '$salutation', '$fname', '$mname', '$lname', '$fname'.', '.'$lname', '$pob', '$dob', '$qualification', '$pg', '$pgy', '$graduation', '$gy', '$schooling', '$sy', '$religion', '$caste', '$subcaste', '$familyname', '$fathername', '$mothername', '$brothers', '$sisters', '$uuid', NOW())";
You'll see I added 'fullname' in the columns list, and this in the values list: '$fname'.', '.'$lname',
using PHP's concatenation operator .
The correct way to accomplish this is to simply concatenate the values and insert them at the very same time you insert the rest of the values. Let me know if that does it for you.
A side note, editing your original code does make the question more confusing for viewers who came in after the edits were made. Consider adding notes about any edits to the code, instead of editing the original example.
I am using the following code which all works fine on my localhost and produces no errors except when I run the file from my server it does not insert the values into my table. I am not getting any errors.
Please can someone show me where I am going wrong? Thanks in advance.
code:
<?php
session_start();
$reference=$_GET['reference'];
include '../../include/config.php';
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = "UPDATE new_supplier_request
SET action_taken='actioned', status = 'Actioned', process_description = 'Supplier Contacted' WHERE reference = '$reference'";
mysql_select_db('hewden1');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not update data: ' . mysql_error());
}
$first = "SELECT * FROM new_supplier_request WHERE reference = '$reference'";
$result = mysql_query($first);
while($row = mysql_fetch_assoc($result)) {
$user1 = substr($row['contact_second_name'], 0, 5) . '';
$user2 = substr($row['contact_first_name'], 0, 1) . '';
$digits = 2;
$num = rand(pow(10, $digits-1), pow(10, $digits)-1);
$digits2 = 8;
$ref = rand(pow(10, $digits2-1), pow(10, $digits2)-1);
$user = $user1.$user2.$num;
$comp_name = $row['company_name'];
$first_name = $row['contact_first_name'];
$second_name = $row['contact_second_name'];
$email = $row['contact_email'];
}
include '../../dependables/secure.php';
$query = "INSERT INTO supplier_users(user_id, user_name, company_name, user_first_name, user_last_name, user_email, user_password_hash, salt, account_created)
VALUES ('".$ref."','".$user."','".$comp_name."','".$first_name."','".$second_name."','".$email."','".$hash."','".$salt."',now())";
$result1 = mysql_query($query, $conn);
$query2 = "INSERT INTO supplier_stats(user_id, user_name, company_name, user_first_name, user_last_name, user_email) SELECT user_id, user_name, company_name, user_first_name, user_last_name, user_email FROM supplier_users WHERE user_id = '$ref' ";
$result2 = mysql_query($query2, $conn);
include '../send_emails/send_email1.php';
mysql_close($conn);
if($result2) {
$_SESSION['success'] = '<div id="bottom_notification"><div id="bottom_notification_heading"><h23>Request Received Successfully!</h23><div class="boxclose" id="boxclose">✖</div></div><p>Thank you, the request was actioned successfully. Your reference is: '.$reference.'.</p><p>Track this request..</div>';
$_SESSION['message2'] = '<div class="message_box_prompt"><div class="boxclose2" id="boxclose2" style="float:right; margin:10px; cursor:pointer; cursor:hand;" onclick="this.parentNode.parentNode.removeChild(this.parentNode);">✖</div><div class="message_box_text"><strong>Success!</strong> Your request was actioned successfully. Your reference is: '.$reference.'.</div></div>';
header('Location: ' . $_SERVER['HTTP_REFERER']);
}else{
$_SESSION['success'] = '<div id="bottom_notification"><div id="bottom_notification_heading"><h23>There was a problem!</h23><div class="boxclose" id="boxclose">✖</div></div><p>We had difficulty processing your request.</p><p>Track this request..</div>';
$_SESSION['message2'] = '<div class="message_box_prompt"><div class="boxclose2" id="boxclose2" style="float:right; margin:10px; cursor:pointer; cursor:hand;" onclick="this.parentNode.parentNode.removeChild(this.parentNode);">✖</div><div class="message_box_text"><strong>Oooops!</strong> There was a problem processing your request.</div></div>';
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
?>