Cannot get results from my custom table anymore - php

I was able to get results before and add into, I am not sure if my SELECT code broke or my INSERT broke, or if my table is not being created anymore.
I just want my table to get populated with new inserts into the db table, better yet inserts to work. Below is my current code for the two main files i'm using. To test I use a FTP to move over my updated files, refresh, and try the buttons. As I said before they worked perfectly fine, up until I created the uninstall function which drops my table.
SOLVED: Whole issue was I made a typo in my table creation code, probably hit backspace by accident.

You need to close the $wpdb->insert();
Try this:
$query = $wpdb->prepare( "insert into {$table_name} (name,category,url) values (%s,%s,%s)",'test,'test','test');
$inserted = $wpdb->query( $query );
if( $inserted ){
echo 'ok';
}else{
echo 'error';
}
instead $wpdb->insert

Related

When Renaming table name, it is actually dropping table from Mysql - Using PHP

I am running into a very weird problem in my PHP script when trying to rename my table name. Instead of renaming it, it is dropping it.
Background Info:
I am running the script on my browser
Rename to the original table name happens right after the drop of original table.
Code Example:
$tableSuffix = "1";
$tableSuffix2 = "2";
// This table already exists "testing_$tableSuffix LIKE template_testing"
// This is being created
CREATE TABLE testing_$tableSuffix2 LIKE template_testing
$queryString = "DROP TABLE testing_$tableSuffix";
$query = $db->query($queryString);
$queryString = "RENAME TABLE testing_$tableSuffix2 TO testing_$tableSuffix";
$query = $db->query($queryString);
Renaming is not happening and both testing_$tableSuffix and testing_$tableSuffix2 are dropping and getting deleted.
Looks like I figured out the bug. Not sure yet though why it is happening on Chrome only and not Safari. My script is running 2 instances within 2 seconds on Chrome, however this issue does not happen on Safari.

Inserting survey into SQL database with PHP

I have an HTML survey. I am handling it with PHP and passing it with PHP into a MySQl database. Before this section of code, I post every input, and echo it out as a summary. Every input is reading correctly in the summary, so the form seems to be working fine. I manually input 1 dataset to test the database columns, and then 1 set of data went straight from the form to the database without issue. Now, however, I tried to insert another set of data and it isn't uploading.
I have each field outlined because I have another field that is an autoincrement for when a row is inserted. On a previous form handle I did, I also had an autoincrement field that worked perfectly without including it in the insertion process, so I'm fairly certain I don't need to include it here.
Is there something in the insert code that I've overlooked? I can manually input results just fine that match exactly what I put into the survey fields, but the digital upload from survey submission to database is not being completed. I AM connected to the database, because I have an error for failed connection set up that isn't popping up (it is paired with $dbcon. $dbcon stands for database connection).
//Data Insertion
$res_ins = "INSERT INTO Survey (name, zip,
gender, income, savings, disaster, work,
res_road, work_road, evacuation, lodging,
injury, children, num_child, educ, city_prep,
PrepComments, emer_res, info, prep, fut_prep)
VALUES ('$name', '$zip', '$gender', '$income',
'$savings', '$disaster', '$work', '$res_road',
'$work_road', '$evacuation', '$lodging',
'$injury', '$children', '$num_child', '$educ',
'$city_prep', '$PrepComments', '$emer_res',
'$info', '$prep', '$fut_prep')";
$insert = $dbcon->query($res_ins);
//Terminate connection to database and end
insertion
mysqli_close($dbcon);
I can't comment because of reputation, so I have to give you a hint in the answer: did you try to use this query directly on your database, using some interface?
However, you could try to add some rows to see what is the error, before to close the connection:
if ($dbcon->query($res_ins) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $res_ins. "<br>" . $dbcon->error;
}
before executing, print the query. it will help you to find out the root cause. most common reason of this type of issue is special character. You can check is there any special character in your query.

Why my code is storing data in Mysql database for almost eleven times?

I am having some problem with the mysql. Well this is my code
class store_info{
function store_info(&$bean, $event, $arguments){
$id = $bean->id;
$name = $bean->user_name . ' ' .$bean->last_name;
$user_hash = $bean->user_hash;
$query1 = "INSERT INTO sohan_password_management (id_user, name, user_password, register_date, prompt_date, deadline) VALUES('$id', '$name', '$user_hash', NOW(), NOW() + INTERVAL default_prompt_date DAY, NOW() + INTERVAL default_deadline DAY)";
$result1 = $bean->db->query($query1, true);
$bean->save();
}
}
?>
whenever i run this code, data get store in database but it stores for 11 times. I mean same data get stored for more than one time in database. May I know what is wrong here? Till yesterday it was working fine. I don't know what happened to this now.
Try to comment/ remove $bean->save();
I think it is a after_save logic hook and it is executing multiple times.
Also, check this post as well.
http://support.sugarcrm.com/02_Documentation/04_Sugar_Developer/Sugar_Developer_Guide_6.5/03_Module_Framework/Logic_Hooks/Examples/Preventing_Infinite_Loops_with_Logic_Hooks/
I've had a similar issue before. It was due to a slow network connection and reloading the same page which kept submitting my input to the table. To fix this, I created a if statement to check the values of each item being submitted and if a certain number were the same values as a current column in the table, I told it not to input the data.
After including this if statement, it worked perfectly for me.
Not sure if you can apply the same solution to your data set, but I hope this helps.
You are calling bean save in the logic hook which is why bean is saving records multiple times.
You do not need to call the save function within a hook.

Check for duplicate when editing a record in MySQL

I'm having problems with a piece of code and wonder if someone can help.
I have a form that submits information to a MySQL database, I have the correct code for checking to see if the submitted product code already exists, and if so shows a warning message and the record is not added.
That code is:
$result = mysql_query("SELECT * FROM listing_1 WHERE product_code='$product_code'");
$num_rows = mysql_num_rows($result);
if ($num_rows) {
adminwarnmessage("DUPLICATE REFERENCE CODE","FAILURE - <b>$product_name</b> has <b>NOT</b> been added because the reference number already exists.");
}
That works fine for Data Entry, however I have another form that allows users to edit the record, this is what is causing me a problem, as the above code only tells me that there is already a matching record in the database, Of course when I try to save (update) the record it now tells me I can't because it is a duplicate.
What I would like to happen is that it doesn't allow users to choose another productcode that already exists, but I want them to be able to update the record using the same product code the form fetched from the database.
Hope that makes sense, any help greatly appreciated.
If you have id (primary key) then You will have to compare with id of that product before updating the record. For example
$result = mysql_query("SELECT * FROM listing_1 WHERE product_code='$product_code' AND id!=$id");
$num_rows = mysql_num_rows($result);
if ($num_rows) {
echo "duplicate record";
}
Here $id is the id of the product that you should have while editing the record.
following is the step you need to follow when you managing the Database
First you need an primary key(auto_increment) in "ID" field
When you execute insert query that time first check where record is already available or not. if not available than only you should execute insert query.
use primary key filed for update, delete etc...
if you follow the above step than you never face this problem
Are you perhaps checking for duplicate occurrences in both of insert and update statement? If so, you shouldn't. Duplicate entry is relevant only when "inserting". You shouldn't use the same check for update. Hope that helps.
why the same code for update also? you can use another query for updating which is better for debugging if you have problems later. try this
$result = mysql_query("UPDATE listing_1 SET product_code='$new_product_code' WHERE product_code='$product_code' AND id='$id'");
if($result) {
echo "your product was updated.";
} else {
echo "your product is not in DB";
}
EDIT: be careful in updating or inserting things, take always id to check unless your product_code is unique
EDIT
I have resolved this issue by making $productcode field a unique Index.
Now when editing if there is a duplicate..
Mysql does not accept the update query, it returns an error code
I trap that error code and include it in an if statement...
if( mysql_errno() == '1062' )
adminwarnmessage("DUPLICATE REFERENCE CODE","FAILURE - $product_name has NOT been added because the reference number already exists");
}
adminmessage("Item Updated", Congratulations you updated $productname succesfully");
}
This now allows editing of $productcode but does not allow it to be changed to one already used in the database.
Thank you to everyone who took the time to offer help

How get value from POST

This piece of code has been tripping me out for the past four hours. It is deleting a row of photos by the primary ID.
I have var_dump($selectedPhoto) and it is the correct ID, a number. My code will run every time I press delete photo, get to the mysqli_stmt_store_result part and shoots out the $txtMessage, But the database does not update.
This is really weird because I have used the exact same code, with different variables on another page and it works perfectly fine.
Can you see any errors by looking at this? OR have a better way to writing the delete statement.
if (isset($_POST['btnDeletePhoto']))
{
$selectedPhoto = $_SESSION['selectedPhoto'];
$deleteString = "DELETE FROM Photos WHERE PhotoID = ?";
$preparedDeleteStmt = mysqli_prepare($link, $deleteString);
mysqli_stmt_bind_param($preparedDeleteStmt, 'i', $selectedPhoto);
if (!mysqli_stmt_execute($preparedDeleteStmt))
{
mysqli_close($link);
die("The system is not available, try again later");
}
if(mysqli_stmt_store_result($preparedDeleteStmt))
{
$txtMessage = "Delete successfull";
}
To add: $selectedPhoto is a value of a select, drop down list value.
If the photo comes from the value of a select, it is not going to be stored in a session variable, so you probably need to change:
$selectedPhoto = $_SESSION['selectedPhoto'];
to:
$selectedPhoto = $_POST['selectedPhoto'];
Apart from that you need to add error handling to all database operations.

Categories