PHP Ticket System - php

I'm making a VERY basic ticket system. Where basically if an SQL query or something doesn't run properly. It will insert a few bits of data into a MySQL table so the admin can run through and see what has happened and try to fix it.
<?php
$stamp = gettimestamp();
$error = $db->error;
$page = basename($_SERVER['PHP_SELF']);
echo "Error Updating! Please contact your IT Admin Team!";
echo "Stamp: ".$stamp;
echo "Error: ".$error;
echo "Page: ".$page;
echo "Email: ".$email;
$ticketSQL="INSERT INTO `db744544270`.`supportTickets` (`timestamp`, `problem`, `page`, `user`) VALUES ('$stamp', '$error', '$page', '$email')";
if ($db->query($ticketSQL) === TRUE) {
echo '<script>alert("Record Upated!");</script>';
header( "refresh:5;url=index.php" );
//var_dump($sql);
//echo "<script>location.href = 'index.php';</script>";
} else {
echo "Error AGAIN!";
}
?>
Above is the code I am running and basically I have put this into a file and just include that file on every page where I need it. For example inside the error handling of an if statement. If query TRUE-> YAY, else query FALSE->include ticket page.
Like I said...very basic..
Whenever I run the page and at the moment I am getting an error from another query but that's a question for a different time :) this is the current error i get...
Error updating record: Unknown column '1' in 'where clause'
Fatal error: Call to undefined function gettimestamp() in /homepages/38/d735513801/htdocs/includes/supportTicket.php on line 2
I have no idea why it is saying it's undefined. I have tried changing the type and everything. It just will not insert into the table, I feel like I'm being stupid but I can't see why it's not working...

Related

PHP If Else statement not working for database

Ok, I'm confused. I have some code that searches a database table for a username, and then uses an if else statement to run some code depending on if the user is found or not. My code is below. The problem is that the code isn't even seeing the if else statement, and I have no idea why. Any help is appreciated.
$sqluser = "select * from users where username='" . $user ."'"; //Searching to see if the user is in the database
echo $sqluser . "<br><br>"; //writes out the select statement to make sure it is correct
$query = mssql_query($sqluser); //returns the results
$num_rows = mssql_num_rows($query); //gets the number of rows returned
echo $num_rows; //writes out the number of rows
if ($num_rows==0) //determines what happens next if the user exists or not
{
//displays an error box if the user doesn't exist
echo "<script type=text/javascript>";
echo "alert('That user doesn't exist. Please try again.')";
echo "</script>";
}
else
{
//will be code to run if the user does exist
echo "<script type=text/javascript>alert('Testing.')</script>";
}
I couldn't add a comment. So I will write this as an answer instead.
Since you state that the alert JavaScript is showing in the page source, this mean that the IF/ELSE statement in PHP is working fine. The problem is with the single quote. You have a single quote inside a single quoted alert function. Hence the JavaScript alert function cannot be executed.
echo "alert('That user doesn't exist. Please try again.')";
Try using this instead
echo "alert('That user doesn\'t exist. Please try again.');";

Record not getting updated in PHP/MySQL

I am trying to change some values in a table but for some reason the values are not getting updated. Here is the php :
$title=mysqli_escape_string($conn,$_POST['title']);
echo $title."<br>"; //Test
$note=mysqli_escape_string($conn,$_POST['jnote']);
echo $note."<br>"; //Test
$query="UPDATE articles SET return_j='1' AND j_note='$note' WHERE article_name='$title'";
if ($conn->query($query) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
Problem is I am getting no error. 'Record updated successfully' is what I get. So the query is being executed successfully but the table is not getting updated. I don't understand why.
When I am updating other columns of the table (in some other scripts), I am having no problem.
Swap out the AND for a comma.
UPDATE articles SET return_j='1', j_note='$note' WHERE article_name='$title'
and I don't think you need
===TRUE
just leave it out of the condition statement.

PHP MySQL record update error

I am wondering what mistakes has been made in this pretty simple update statement using old version of PHP. If I echo the statement it says update statement is getting form submitted properly.
Here is the code:
<?php
echo $q = "UPDATE notice SET FromDate = $notice_fromdate, ToDate = $notice_todate, VacType ='$notice_vactype',NoticeDetail ='$notice_detail',Status ='$notice_status' WHERE ID=$id";
if (mysql_query($link, $q)) {
echo "Record updated successfully";
} else {
echo "<h3>Error updating record</h3>". mysql_error($link)."-". mysql_errno($link). "\n";
}
?>
and the output returns this
UPDATE notice SET FromDate = 2017-01-08, ToDate = 2017-01-09, VacType ='May Day',NoticeDetail ='Testing',Status ='Enabled' WHERE ID=3
Error updating record
-0
I know its a pretty simple thing, I guess I have not made any mistake in the update statement but instead it is showing Error update record. I copied the output SQL statement and run at phpmyadmin, it has worked properly. It would be nice if you can help me. Thank in advance
Note: Clients website built on old version of PHP, I know that few functions got deprecated so it would be better if you do not discuss or criticize about the version.
Apply quotes to dates it will work
<?php
echo $q = "UPDATE notice SET FromDate = '$notice_fromdate', ToDate = '$notice_todate', VacType ='$notice_vactype',NoticeDetail ='$notice_detail',Status ='$notice_status' WHERE ID=$id";
if (mysql_query($link, $q)) {
echo "Record updated successfully";
} else {
echo "<h3>Error updating record</h3>". mysql_error($link)."-". mysql_errno($link). "\n";
}
?>

mysql Duplicate error handling

I'm trying to use PHP to enter data from a form. When I try to enter duplicate data a bad message pops like
Something went wrong with this:
INSERT INTO customer VALUES('jamie9422','Jamie Lannister','sept of baelor','jamie#cersei.com',9422222222,0) Duplicate entry 'jamie9422' for key 'PRIMARY' "
Instead, I want to display a clean error message. How can I do that. Here's my code I've written so far...
<?php
include_once "dbConnect.php";
$connection=connectDB();
if(!$connection)
{
die("Couldn't connect to the database");
}
$tempEmail = strpos("{$_POST["email"]}","#");
$customer_id=substr("{$_POST["email"]}",0,$tempEmail).substr("{$_POST["phone"]}",0,4);
//$result=mysqli_query($connection,"select customer_id from customer where customer_id='$customer_id' ");
//echo "customer_id is".$result;
$query = "SELECT * FROM CUSTOMER WHERE CUSTOMER_ID='$customer_id'";
$customer_idcip = $customer_id-1;
echo $customer_idcip;
if ( mysql_query($query)) {
echo "It seems that user is already registered";
} else {
$command = "INSERT INTO customer VALUES('{$customer_id}','{$_POST["name"]}','{$_POST["address"]}','{$_POST["email"]}',{$_POST["phone"]},0)";
$res =$connection->query($command);
if(!$res){
die("<br>Something went wrong with this:{$command}\n{$connection->error}");
}
echo "Welcome ".$_POST["name"]." \nCongratulations on successful Registration. Refill your Wallet here";
//$cutomerRetrival = mysql_query("select from customer where customer_id='$customer_id'");
echo "<br>Please note your customer ID :".$customer_id;
}
/*if($result)
{
echo "Query Fired";
$dupentry = mysqli_num_rows($result);
if($dupentry==1)
{
echo "You are already Registered";
exit;
}
}*/
?>
The error code (number) is 1022.
You can e.g. define a constant for that (so that somebody else in x months has a chance to understand the code) like
define('ER_DUP_KEY', 1022);
and then do something like
if(!$res){
if ( <error code>==ER_DUP_KEY ) {
handleDuplicateEntryError();
}
else {
die("<br>Something went wrong with this:{$command}\n{$connection->error}");
}
}
since I don't know how $res =$connection->query($command); works (and what $connection is I can't tell you exactly how to implement <error code>==ER_DUP_KEY, could be by using mysql_errno.
But it seems to be somehow intermingled with mysql_query($query), i.e. the old, deprecated mysql_* extension and some custom class. You might want to fix that first.... ;-)
see http://docs.php.net/manual/en/mysqlinfo.api.choosing.php
Your code doesn't check for existing record properly
Change
if (mysql_query($query)) {
echo "It seems that user is already registered";
}
to
$result = mysql_query($query);
if (mysql_num_rows($result)) {
echo "It seems that user is already registered";
}
Also, PLEASE do not use $_POST variables without escaping them first, use something like mysql_real_escape_string() to escape each variable passed from the user, otherwise your website will be hacked really fast with SQL Injection.
Make some update into your and then try to get error message 'customer already registered.'
$query = "SELECT * FROM CUSTOMER WHERE CUSTOMER_ID='$customer_id'";
$res= mysql_query($query);
$customer_count = mysql_num_rows($res);
$customer_idcip = $customer_id-1;
echo $customer_idcip;
if ( $customer_count > 0 ) {
echo "It seems that user is already registered";
} else {
...................................
Thank you all.
Actually I was using mysqli API in my connectDB.php file..
Hence I needed to call functions on mysqli.
Instead I was calling mysql. i.e I was creating a new connection, thus the query wasn't getting fired at all.
Changed to mysqli->query($result) that is object oriented style
and it worked fine....
Use Try Catch instead.
try{
$res =$connection->query($command);
}catch(Exception $e){
die( "Write your error appropriate message here");
}

In yii how to get success message after executing sql

i need to know how to get success message after successful execution of sql or failure message of wrong execution.my example is below
`
public function actionSql()
{
$table_no='1';
$employee='1';
$status='1';
$connection=Yii::app()->db;
$sql="INSERT INTO orders_transaction (table_no,employee,status) VALUES(:table_no,:employee,:status)";
$command=$connection->createCommand($sql);
$command->bindParam(":table_no",$table_no,PDO::PARAM_STR);
$command->bindParam(":employee",$employee,PDO::PARAM_STR);
$command->bindParam(":status",$status,PDO::PARAM_STR);
$command->execute();} `
after executing i need to know is the row successfully inserted or not.
i used below one but no use its only echoing successfully not failure
if($command->execute())
{
echo "Successful";
}
else {
echo "ERROR";
}
so i tried this one its giving permission denied error for localhost with password" "
$result=mysql_query($sql);
// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
}
else {
echo "ERROR";
}
i hope you under stand my problem.please give any suggestion or answer.
Execute()
returns the number of affected rows(for INSERT, DELETE, UPDATE etc).
$num = $command->execute();
here $num will contain the affected number of rows.

Categories