I run this function and the value of the post form doesn't insert into the DB. I'm not getting any error messages but I checked and saw that $this->user_id is populating. Am I missing something else?
function senddata () {
$con = mysql_connect("localhost","root","XXXXX");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("user", $con);
$sql="INSERT INTO profile (collegemajor) VALUE('$_POST[major]') WHERE userid=$this->user_id";
mysql_close($con);
}
INSERT statement don't have WHERE clausule,
you need to send query!!!
First of all, I don't see the mysql_query() - the function to execute the query.
It seems like the problem is here:
$sql="INSERT INTO profile (collegemajor) VALUE('$_POST[major]') WHERE userid=$this->user_id";
Use $sql="INSERT INTO profile (collegemajor) VALUE('$_POST[major]'); instead.
The code:
function senddata () {
$con = mysql_connect("localhost","root","XXXXX");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("user", $con);
$sql="INSERT INTO profile (collegemajor) VALUE('$_POST[major]');";
mysql_query( $sql , $con );
mysql_close($con);
}
This should be an update function, it seems, rather than an insert function (if you're trying to update a row that already exists, which is seems you are based on the "Where" clause).
And, as others have mentioned, you never ran mysql_query() or similar function.
Related
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]')";
Here is the code:
<?php
$con=mysql_connect('localhost', 'itorras', 'passwordhere');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db( "my_db" ) or die( 'Error'. mysql_error() );
$sql="INSERT INTO Brackets(have things here)
VALUES(and here)";
if (!mysqli_query($con,$sql))
{
die('Error with adding row: ' . mysqli_error($con));
}
echo "Thank you, your bracket has been submited.";
echo "<a href='index.html'>Click here to go back to home page</a>";
?>
I am using the username and password that is given the rights to mess with the database.
So when i try to submit something into the file none of the top errors run but then the bottom error prints error with adding row and nothing more. This file worked fine on my local server but has not worked on the web host. I am using godaddyweb hosting, so phpmyadmin and cpanelx.
If you need any more info let me know. Have been at this for a couple hours.
You are using mysql_connect but mysqli_query. You are mixing mysql and mysqli. Use only mysqli_*.
It appears you are using two different libraires at the same time
The original mysql API is deprecated for various reasons and you should not use it in new code
Instead, use PDO or mysqli
In your code, you are using the original mysql for the db connect, and then you use mysqli for the query. Instead you should only use mysqli
Replace the following code :
$con=mysql_connect('localhost', 'itorras', 'passwordhere');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
With :
$db = new mysqli('localhost', 'user', 'pass', 'demo');
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
Then you can query the database with something similar :
$sql = <<<SQL
SELECT *
FROM `users`
WHERE `live` = 1
SQL;
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
For a complete overview, you can take a look at the documentation
http://ca2.php.net/mysqli
Can't quite figure this one out. Not posting to MySQL. Not getting errors, just not posting.
<?php
$con = mysql_connect("localhost","XXXX","XXXX");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("XXXX", $con);
for( $i = 1; $i <= $count; $i++ )
{
$newtest1 = $_POST['test1'.$i];
$newtest2 = $_POST['test2'.$i];
$newtest3 = $_POST['test3'.$i];
}
$sql="INSERT INTO database (test1,test2,test3) VALUES ('".$newtest1."','".$newtest2."','".$newtest3."')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
?>
Tried ('$_POST[test1]') and (' . $_POST['test1'.$i] . ')
$sql="INSERT INTO database (test1,test2,test3) VALUES ('".$newtest1."','".$newtest2."','".$newtest3."')";
Here there is a "table" which you want to insert a data.
You may get the idea of using $sql wrong.
I'll try to explain it from an example photo: PHOTO
As you see i have a database called "test" and a table in it which im inserting my data.
You should use this code and connect to that database:
mysql_select_db("XXXX", $con); //to select "my" database (on the example) type "test" to "XXXX"
The "database" you wrote here:
$sql="INSERT INTO database (test1,test2,test3) VALUES ('".$newtest1."','".$newtest2."','".$newtest3."')";
must be changed to your selected database. In this case you just wrote "XXXX" to that.
So the final code is:
$sql="INSERT INTO XXXX (test1,test2,test3) VALUES ('".$newtest1."','".$newtest2."','".$newtest3."')";
For some reason I am getting the error
No database selected
I have no idea why this is not working, am I being blind?
$con = mysql_connect('localhost','myusername','mypassword');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('mydatabase');
There are no typos in the actual username, password or database name and I can't see a mistake in my code.
Suggestions are very much appreciated.
Thanks
Do you have username added to the user table? What if you try the same code with root user?
try:
mysql_select_db('mydatabase', $con);
in stead of:
mysql_select_db('mydatabase');
My code doesn't insert any records to mysql. What is wrong? I am really confused.
I have designed a form and I want to read data from text box and send to the database.
<?php
if(isset($_post["tfname"]))
{
$id=$_post["tfid"];
$name=$_post["tfname"];
$family=$_post["tffamily"];
$mark=$_post["tfmark"];
$tel=$_post["tftell"];
$link=mysql_connect("localhost","root","");
if (!$link)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("university",$link);
$insert="insert into student (sid,sname,sfamily,smark,stel) values ($id,'$name','$family',$mark,$tel)";
mysql_query($insert,$link);
}
mysql_close($link);
?>
You'd better to put quotation mark for id, mark and tel after values in your query. Also as #Another Code said, you must use $_POST instead of $_post in your code. Try this and tell me the result:
<?php
if(isset($_POST["tfname"])) {
$id=$_POST["tfid"];
$name=$_POST["tfname"];
$family=$_POST["tffamily"];
$mark=$_POST["tfmark"];
$tel=$_POST["tftell"];
$link=mysql_connect("localhost","root","");
if (!$link) {
die('Could not connect: ' . mysql_error());
} else {
mysql_select_db("university",$link);
$insert="insert into student
(sid,sname,sfamily,smark,stel) values
('$id','$name','$family','$mark','$tel')";
mysql_query($insert,$link) or die (mysql_error());
mysql_close($link);
}
} else {
die('tfname did not send');
}
?>
Use mysql_query($insert,$link) or die (mysql_error()); to fetch the error message.
With the code you've provided it could almost be anything - to do some tests... have you echo'd something to confirm you are even getting the tfname in POST? Does it select the database fine? Do the fields $id, $mark, and $tel need single quotes around them as well? We need to know more about where the code is not working to provide more help but that snippet appears as though it should be running, in the interim, please use some echo's to narrow down your problem!
Try to run the generated sql query in the sql query browser. Get the query by "echo $insert" statement.
change the $insert to:
$insert="insert into student (sid,sname,sfamily,smark,stel) values ($id,'".$name."','".$family."','".$mark."','".$tel."')";
further set ini_set('display_errors',1) so that php displays the error messages as required.
Lastly, whenever doing a mysql query, try to use or die(mysql_error()) in the query so that if somethings wrong with mysql or syntax, we are aware.
$q = mysql_query($query) or die(mysql_error());