MySQL DB Column Not Updating after TinyMCE post INSERT - php

I've managed to implement a simple TinyMCE editor on my site and have it call using mySQL database. My problem Im having now is getting the DB to overwrite the contents already stored inside with what is being posted?
I simply want to overwrite the table contents (within the column) with what is being inserted.
Here is my code:
<!--- CONNECT TO THE DATABASE------>
<?php
$con = mysql_connect("localhost","root","jdkldk8%by");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("cms", $con);
$sql="INSERT INTO tinymce (contents, contact, slider, resources)
VALUES
('$_POST[contents]','$_POST[contact]','$_POST[slider]','$_POST[resources]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con);
?>
<!--- END DATABASE SETTINGS ----->
[ View your changes here ]
///////////////////////////////////////////////////////////////////////////////////////
Well... i tried again but its still not posting all fields to the DB! only updating 1.
<!--- CONNECT TO THE DATABASE------>
<?php
require_once('db.php');
$contents=$_POST['contents'];
$contact=$_POST['contact'];
$slider=$_POST['slider'];
$resources=$_POST['resources'];
$id='1';
$sql="UPDATE tinymce SET `contents`='$contents', `contact`='$contact', `slider`='$slider', `resources`='$resources' WHERE id='$id'";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "Saved!";
mysql_close($con);
?>
<!--- END DATABASE SETTINGS ----->

You want to use UPDATE, not INSERT, to update a previously created MySQL column.
Try editing a column using phpmyadmin and copying the resulting code from their MySQL code viewer.

Related

Adding to Column in SQL issues (in photo-gallery website)

I'm building a photography website where I have one page that displays the pictures (bootstrapped and all) and then another page so I can upload new photos (consists of an html page with form and php that runs download code). In this php page I want to make an SQL addition to my column name in database photos. I want to add the file name into the SQL database name so I can later access it in my page displaying the photos and I can get the file name so I can for loop it there for all the pictures in the directory. The issue I'm having is adding the target file into the name and then getting it. Here's my code for adding the filename:
$link = mysql_connect('meadowebcom.ipagemysql.com', 'lphotos', 'gut2*EMI_12');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_select_db(photos);
$sql = 'INSERT INTO `name` (`target_file`) VALUES ('.basename( $_FILES["fileToUpload"]["name"]).');';
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
mysql_close($conn);
I have it display that I am connected but can't add anything. I haven't even started on getting the file in the other file. I don't usually do PHP outside of contact and registration forms so any help or any points in the right direction would help.

how to insert html table data per tr into sql table

here is my following code, I want to insert html table data per row into sql, but using this code, only store last trow to database table, I think I need to use loop to read the data per trow, not only last trow, any ideas?
<?php
//connect
if (isset($_POST['submit'])){
$con = mysql_connect("localhost","Matt","password");
if (!$con){
die("Can not connect: " . mysql_error());
}
mysql_select_db("propereva",$con);
INSERT INTO properevatables (Number, Name,BasicSalary,ServiceMonths,SalaryPercentWithoutCEO,SalaryPercentWithCeo,PercentEvaShareByCeoDirectReports,PercentEvaShareByCeoAndDirectReports,PercentKpi,EvaBonusInMonths,percentwithbank,TotalEvaBonus,totalwithbank) VALUES ('$_POST[number]','$_POST[membername]','$_POST[salary]','$_POST[service]','$_POST[percentwithoutCEO]','$_POST[percentwithCEO]','$_POST[ceoReport]','$_POST[directresult]','$_POST[kpi]','$_POST[resultpercentageEVA]','$_POST[percentwithbank]','$_POST[yearonly]','$_POST[totalwithbank]')";
mysql_query($sql,$con);
mysql_close($con);
}
?>
What you are trying to do is send the data of an entire table to the server for storage.
You can do this using JavaScript and collate all the data into a JSON Object to send to the server using an AJAX request.
You can learn about AJAX in JavaScript from
here https://developer.mozilla.org/en-US/docs/AJAX/Getting_Started
or you can chose to use a JavaScript library such as JQuery to help you with your AJAX needs.
http://api.jquery.com/jquery.ajax/
You can also use HTML inputs within the table to submit all the values however this is not recommended.
<?php
include 'connect.php';
if(isset($_POST['submit']))
{
$number=$_POST['Number'];
$name=$_POST['Name'];
$salary=$_POST['BasicSalary'];
$service=$_POST['ServiceMonths'];
$insert=mysql_query("insert into `properevatables ` (Number,Name,BasicSalary,ServiceMonths) values('$number','$name','$salary','$service')");
if($insert)
{
$msg="Inserted Successfully";
echo "<script type='text/javascript'>alert('$msg');</script>";
}
else
{
$msg=" Insert Not Successfully";
echo "<script type='text/javascript'>alert('$msg');</script>";
}
}
?>
By this you can add more fields.I added only three as of now.Here the connect.php is the connection file to the database.You can either use separate file else include he connection code in the same file.

Having trouble getting two fields to concatenate

hostSo i know how to get the two fields to concatenate from directly inside of MYSQL, but having trouble getting it to work with my PHP.
Directly from MYSQL = SELECT CONCAT(ConfigurationItem, ' - ', ,Buzzword) FROM Buzz;
But how do i incorporate it into this PHP below, I have researched to no end. I want to combine the two fields ConfigurationItem and Buzzword into a field named shortdescription, without having to do it manually through MYSQL everytime the PHP is submitted.
<?php
$con = mysql_connect("host","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("buzz_feed", $con);
$sql = "INSERT INTO Buzz (BuzzID, ConfigurationItem, Buzzword, OccurrenceDate, PostingDate, TierStatus, MasterTicket)
VALUES
('$_POST[BuzzID]','$_POST[ConfigurationItem]','$_POST[Buzzword]','$_POST[OccurrenceDate]','$_POST[PostingDate]','$_POST[TierStatus]','$_POST[MasterTicket]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "Buzz Phrase information updated";
mysql_close($con)
?>
I've concatenated them together in php as the insert.
Although there is nothing wrong with catting them in your select statement.
In fact I'd opt for that because it is redundnant-y, you are inserting the same data twice in essence.
But this should do what you are asking for.
I have also corrected your quotation marks in the query.
Also google sql injection
<?php
$con = mysql_connect("host","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("buzz_feed", $con);
$sql = "INSERT INTO Buzz (BuzzID, ConfigurationItem, Buzzword,
OccurrenceDate, PostingDate,
TierStatus, MasterTicket, shortdescription)
VALUES
('".$_POST['BuzzID']."','".$_POST['ConfigurationItem']."',
'".$_POST['Buzzword']."','".$_POST['OccurrenceDate']."','".$_POST['PostingDate']."',
'".$_POST['TierStatus']."','".$_POST['MasterTicket']."',
'".$_POST['ConfigurationItem']."' - '". $_POST['Buzzword']."')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "Buzz Phrase information updated";
mysql_close($con)
?>
I ended up resolving my issue by inserting "ShortDescription" in the INSERT INTO line and then just telling it to insert the two fields I wanted together in the field "ShortDescription" and by using double spaces between my hyphen, I was able to get the desired effect I was looking for which turns out like this "Example - Example" See my code below
$sql = "INSERT INTO Buzz (BuzzID, ConfigurationItem, Buzzword, OccurrenceDate, PostingDate, TierStatus, MasterTicket, ShortDescription)
VALUES
('$_POST[BuzzID]','$_POST[ConfigurationItem]','$_POST[Buzzword]','$_POST[OccurrenceDate]','$_POST[PostingDate]',
'$_POST[TierStatus]','$_POST[MasterTicket]','$_POST[ConfigurationItem]' ' - ' '$_POST[Buzzword]')";

Blank Field in Mysql After Uploading Data

I was uploading data from my android application to a PHP file then inserting it to Mysql database, the problem i am having that i buy a new hosting plan and when i configured everything in the new hosting, i try to upload data from the application it shows only blank fields in the table, i am sure that there's no problem in the PHP or the android code, cause it was working fine and great with the old hosting.. i tried to change the encoding but same issue.
Here's the PHP file:
<?php
$con = mysql_connect("HOST","USER","PASS");
if (!$con)
{
die('Could not Connect:'. mysql_error());
}
mysql_select_db("TABLE",$con);
mysql_query ("INSERT INTO table (rep_desc,dateT) VALUES ('".$_REQUEST['report_Desc']."','".$_REQUEST['Date_Time']."')");
mysql_close($con);
?>
Thanks in advance
If there is any auto_increment column in table. Better check if its auto_increment flag not got uncheck.
Please also check length of database fields and data that you actually trying to insert.
Name of all request variables, columns, tables should be in proper case if it is a UNIX system.
get all the tables by something like this query ...
$sql = "SHOW TABLES FROM $dbname";
$result = mysql_query($sql);
if (!$result) {
echo "DB Error, could not list tables\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
while ($row = mysql_fetch_row($result)) {
echo "Table: {$row[0]}\n";
}

my php adds a blank entry before i even press submit, any ideas?

<DOCTYPE! HTML>
<html>
<body>
<form action="index.php" method="post">
title: <input type="text" name="title" /><br><br>
name: <input type="text" name="name" /><br><br>
<input type="submit" />
</form>
<?php
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("msg360db1", $con);
$sql="INSERT INTO members (title, name)
VALUES
('$_POST[title]','$_POST[name]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
echo mysql_error();
mysql_close($con)
?>
</body>
</html>
this is my form that i will enter the information onto then when add is clicked it should add it to my database but instead it adds when the page loads
You must check like:
if(isset($_POST['title']){
$sql="INSERT INTO members (title, name)
VALUES
('$_POST[title]','$_POST[name]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
echo mysql_error();
mysql_close($con)
}
Then it will save record only if there is some data in post.
Check for submit click on page load
if(isset($_POST['submit']))
{
$con = mysql_connect("213.171.200.70","bjenkins","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("msg360db1", $con);
$sql="INSERT INTO members (title, name)
VALUES
('$_POST[title]','$_POST[name]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
echo mysql_error();
mysql_close($con)
}
PHP Code is run on any page on every load time. As such, when you first load the page to type in your data, you are in effect adding the blank entry. Then when you submit the page, a second entry with data is added.
As such you need to check if the page has been submitted or not. Wrap your php code with a check for $_POST.
if (isset($_POST['name']))
{
$con = mysql_connect("213.171.200.70","bjenkins","K3rrydixon");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("msg360db1", $con);
$sql="INSERT INTO members (title, name)
VALUES
('$_POST[title]','$_POST[name]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
echo mysql_error();
mysql_close($con)
}
Also, the mysql_ functions are deprecated - use some newer constructs like PDO or MySQLi.
You have to check, if the submit button was pressed or if it's a normal page view:
if(isset($_POST)) {
// Do your SQL-Stuff here
}
Need to see the beginning of your code. You might be skip checking if $_POST is set and write into database in case it is.
EDIT: Interesting, now I see the beginning of the code.
**But this code is not sanitized and is vulnerable for sql
injection. please sanitize your code.**
//check if both values are set.
if(isset($_POST['title']) && isset($_POST['name'])){
$sql="INSERT INTO members (title, name)
VALUES
('$_POST[title]','$_POST[name]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
echo mysql_error();
mysql_close($con)
}
}
PHP code is executed on the server before the html is rendered by the browser or even generated. In your case, by the time the form gets displayed (or submitted) the php code is already executed.
And since the variables $_POST[title] and $_POST[name] are blank when the php code is executed, a database row with no (blank) values is inserted.
What you need to do is to check whether the form is submitted and execute the insert queries only if the form is submitted. For this you can add conditions as mentioned in other answers

Categories