MySQL INSERT query does not update the DB - php

attent table :
id int(11) primary key
userID int (11) forigen key from users table
date date
start_time text
end_time text
approv enum default 0
my query :
$sql = "INSERT INTO attent ".
"(id,userID,date,start_time,end_time,approv) ".
"VALUES ".
"('NULL','$userid','$date','$start_time','$end_time','NULL')";
$query = mysqli_query($db,$sql);
I got config.php file that help communicate with dB. I did a few insert queries before and all of them work just fine. I can not understand where is my mistake.

Write your query as below:-
"INSERT INTO attent(`id`,`userID`,`date`,`start_time`,`end_time`,`approv`)
VALUES(NULL,'$userid','$date','$start_time','$end_time',NULL)"

Try like this use back tick,because date is reserved word in mysql
$sql = "INSERT INTO `attent` ".
"(`id`,`userID`,`date`,`start_time`,`end_time`,`approv`) ".
"VALUES ".
"('','$userid','$date','$start_time','$end_time','')";

It's easier to understand what's going on if you will try to put such a query manually through e.g. phpMyAdmin
I don't know your table structure but try to skip id attribute as it's often autoincrementable. You can always skip the fields that is not set to NOT NULL.
Like that:
$sql = "INSERT INTO `attent` ".
"(`userID`,`date`,`start_time`,`end_time`,`approv`) ".
"VALUES ".
"('$userid','$date','$start_time','$end_time','')";

Related

Replace variable with query

I am pretty new to using mysql and variables in php.
I have this code
mysql_query("INSERT INTO `forum_threads` (`name`, `byid`, `cid`,
`content`, `time`, `lastreplied_time`, `lastreplier_id`) VALUES
('{$title}', '{$uid}', '{$cid}', '{$content}', '" . time() . "', '" .
time() . "', '{$uid}');") or die(mysql_error());
In my php file, I want byid to be the value of id in my table forum_users. So can I replace {$uid} with something that will get the value from forum_users. Because I don't think {$uid} is working correct.
I found this code
/* Non-existant forum account */ final public function
createForumAccount($uid) {
$getHabboUser = mysql_query("SELECT * FROM
`users` WHERE `id` = '{$uid}' LIMIT 1");
I assume that the function of that code is to get the {$uid} equal the id from the users table, I want to make the {$uid} to equal the id from the forum_users table.
Then I found this code:
final public function getUserData($uid, $var) {
if($this->checkForAccount($uid) == true) {
$check = mysql_query("SELECT `{$var}` FROM `forum_users` WHERE `uid` = '{$uid}' LIMIT 1") or die(mysql_error());
return mysql_result($check, 0);
}
}
That code wants the {$uid} to equal forum_users id. And that is exactly what I want, but it doesn't equal that, it equals the id from the users table instead, I assume it might collide with eachother or something.
How can I solve this? Can I replace {$uid} in my first code, so byid is selected instantly from forum_users? Can I make a new variable that equals forum_users.id?
First of all this sql query makes no sense. However I am also not sure your question either. If your wanting to change byid to id in your table then you must alter the table. But here is a cleaner version of your sql query.
try:
mysql_query("INSERT INTO forum_threads SET name=\"".$title."\",byid=\"".$uid."\",cid=\"".$cid."\",content=\"".$content."\",time=\"".time()."\",lastreplied_time=\"".time()."\",lastreplier_id=\"".$uid."\" WHERE id=\"".$uid."\" LIMIT 1") or die("Error: ".mysql_error());
//You Change ID to byid
to change byid to ID
mysql_query("ALTER TABLE `forum_threads` CHANGE `byid` `id` int NOT NULL")or die("Error: ".mysql_error());
// this will change the column byid to id
Just Remember Mysql_connect() and mysql API are old and not used after php 7 so start to learn mysqli api
http://php.net/manual/en/book.mysqli.php

Listing username with bid items - PHP

I'm fairly new to PHP and I'm trying to make a simple auction website. I think I've run into my first problem.
What I'm trying to do is let a registered user add an item to the auction. I can do this just fine. However, I also need to keep track of the user that put the item up for bidding. I thought I could get the accountid by inserting the accountid from the current session into my table, but I keep getting an error saying accountid is an unknown column in my field list.
Here is the code where I create the table.
$sql = "CREATE TABLE biditems (
itemid INT(100) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
accountid INT(100),
biditem VARCHAR(30) NOT NULL,
biddesc tinytext
)";
And to add the items.
$accountid=$_SESSION['accountid'];
$item=$_POST['item'];
$description=$_POST['description'];
$sql= "INSERT INTO biditems (accountid, biditem, biddesc) VALUES
('$accountid', '$item', '$description')";
Your query looks fine. I have also tested it.
Did you double check that the structure of your table is correct?
If you can insert a row via phpMyAdmin or the MySQL Workbench try this and afterwards run a select query like
$rows = "SELECT * FROM biditems";
while ($row = mysql_fetch_array($rows)) {
var_dump($row);
}
Something like this just to make sure.
You should make some sql injection escape first. And if you did it - you should add the variables properly. I did not wrote you some injection escape. You can see sprintf instructions for example.
<?php
$accountid=$_SESSION['accountid'];
$item=$_POST['item'];
$description=$_POST['description'];
$sql= "INSERT INTO biditems (accountid, biditem, biddesc) VALUES
('" . $accountid . "', '" . $item . "', '" . $description . "')";

Creating table with variable name php mysql

I'am trying to create table with variable name and columns via forms here's my query
$execute = mysql_query('CREATE TABLE '.$tName.'(
'.$cName.' '.$cType.' )');
And it's creating table with name from variable $tName but it doesnt create the columns.
Try this:
$sql= "CREATE TABLE $tName($cName $cType(45),$cName $cType(50))";
//echo $sql;
$op =mysql_query($sql);
OK try this, I think it gives the general idea
<?php
include 'connect-db.php';
$tbl_name='kerry';
$field1='name';
$field2='value';
$field3='date';
mysql_query("CREATE TABLE `{$tbl_name}`
(
$field1 varchar(15),
$field2 int,
$field3 date
)");
?>

php date does not posted in the db table

Hi im trying to hard code and set a date to a variable inorder to insert it in db table, but after all my efforts it always prints 0000-00-00 00:00:00. data type in the date column of the table is just datetime
following is the code i tried
$retval = '2007-04-19 12:50:00';
$str_cols = "gmid, panelID, trackerID, timestamp";
$str_values ="$gmid, $panel_id, $track, $retval";
$table = "tracktable_".$track;
$query = "INSERT INTO $table ($str_cols) VALUES ($str_values)";
can any body help on this to get the assigned date in the db table
timestamp is a keyword so it should be in apostrophe like 'timestamp'.
$query = "INSERT INTO ".$table." (`gmid`, `panelID`, `trackerID`, `timestamp') VALUES ('".$gmid."', '".$panel_id."', '".$track."', '".$retval."')";
For it to function surely. Add backticks to your $table. Also there is no single quotes in your values. Use this for sure it will work.
$query = "INSERT INTO `$table` (gmid, panelID, trackerID, timestamp) VALUES ('$gmid', '$panel_id', '$track', '$retval')";
$query = "INSERT INTO ".$table." (`gmid`, `panelID`, `trackerID`, `timestamp`) VALUES ('".$gmid."', '".$panel_id."', '".$track."', '".$retval."')";
Try
$str_cols = "gmid, panelID, trackerID, `timestamp`";
$str_values ="$gmid, $panel_id, $track, '$retval'";

SQL insert and update in the same time

Hey I have a query that will insert into the table a new data and I want that in the same time update an outher table with the id of the new data that I have entered. ex:
mysql_query("INSERT INTO `test` (`name`) VALUES ('Mark')");
$query = mysql_query("SELECT `id` FROM `test` WHERE `name` = 'Mark'");
$id = mysql_result($query,0);
mysql_quey("UPDATE `test2` SET `test_id` = $id WHERE `name` = 'Mark'");
How do I do it at same time? because doing it this way I only insert the new data and I dont update the other.
Cumps.
Try this :
mysql_query("INSERT INTO `test` (`name`) VALUES ('Mark')");
$id = mysql_insert_id();
mysql_quey("UPDATE `test2` SET `test_id` = $id WHERE `name` = 'Mark'");
I've changed the backticks to single quotes in your first insert for the values, backticks should never be used for field values.
Also I've changed it to use only two queries, the mysql_insert_id() will get the last inserted id without you needing to query it.
Ref : http://www.php.net/manual/en/function.mysql-insert-id.php
First of all, you do not need the select to get the id, there is mysql_insert_id() for that.
Then you have to use a transaction to make both queries feel like executed at the same time:
mysql_query('BEGIN');
mysql_query("INSERT INTO `test` (`name`) VALUES ('Mark')");
$id = mysql_insert_id();
mysql_query("UPDATE `test2` SET `test_id` = $id WHERE `name` = 'Mark'");
mysql_query('COMMIT');
A transaction makes sure both statements are executed, and no other script can come between them in any way.

Categories