i would like to know why i am not able to insert new data to my Database.
in vardump i get good result but nothing is added to my database
Database : c_id = AutoInc. | a_id | u_id | c_head | c_content | c_date
=TIMESTAMP
if (isset($_POST['c_submit'])) {
$sql_najdi_prihlaseneho_uzivatela = "SELECT * FROM users where u_name = '$_SESSION[username]'";
$vysledokHladania = mysqli_query($connect_to_db, $sql_najdi_prihlaseneho_uzivatela);
if (mysqli_num_rows($vysledokHladania) == 1) {
while ($db_data_prihlaseneho_uzivatela = mysqli_fetch_assoc($vysledokHladania)) {
$sql_vloz_komentar = "INSERT INTO comments (a_id, u_id, c_head, c_content, c_date) VALUES ($_GET[a_id], $db_data_prihlaseneho_uzivatela[u_id], $_POST[c_hlavicka], $_POST[c_obsah], 'NOW()')";
mysqli_query($connect_to_db, $sql_vloz_komentar);
}
}
}
You can use date('Y-m-d h:i:s') to store current date time in our DB by PHP code and in your query you just missed to put '' in array index.
Try this one
if (isset($_POST['c_submit'])) {
$date=date('Y-m-d h:i:s');
$sql_najdi_prihlaseneho_uzivatela = "SELECT * FROM users where u_name = '".$_SESSION['username']."'";
$vysledokHladania = mysqli_query($connect_to_db, $sql_najdi_prihlaseneho_uzivatela);
if (mysqli_num_rows($vysledokHladania) == 1) {
while ($db_data_prihlaseneho_uzivatela = mysqli_fetch_assoc($vysledokHladania)) {
$sql_vloz_komentar = "INSERT INTO comments (a_id, u_id, c_head, c_content, c_date) VALUES
(".$_GET['a_id'].",".$db_data_prihlaseneho_uzivatela['u_id']." ,".$_POST['c_hlavicka']." ,".$_POST['c_obsah']." ,$date)";
mysqli_query($connect_to_db, $sql_vloz_komentar);
}
}
}
I thing it will help you.
do you escape the varchar character ?
Other posible error is the date format in database.
Postecho $sql_vloz_komentar for view the insert statement and the structure of table
if (isset($_POST['c_submit'])) {
$sql_najdi_prihlaseneho_uzivatela = "SELECT * FROM users where u_name = '$_SESSION[username]'";
$vysledokHladania = mysqli_query($connect_to_db, $sql_najdi_prihlaseneho_uzivatela);
if (mysqli_num_rows($vysledokHladania) == 1) {
while ($db_data_prihlaseneho_uzivatela = mysqli_fetch_assoc($vysledokHladania)) {
$sql_vloz_komentar = "INSERT INTO comments (a_id, u_id, c_head, c_content, c_date) VALUES ($_GET[a_id], $db_data_prihlaseneho_uzivatela[u_id], '$_POST[c_hlavicka]', '$_POST[c_obsah]', 'NOW()')";
mysqli_query($connect_to_db, $sql_vloz_komentar);
}
}
}
Related
I'm trying to catch all data from MySQL to check data already exists or not
if data already exists then it should update else insert new data.
I have 7 to 8 conditions in where clause, some of them are null
now the issue is
when i run query its not catching null column thats why its insert new data instead of updating
below is my code
$fetch_stock = mysqli_query($connection,
"SELECT *
FROM `book_stock`
WHERE book_name = '$book_name'
AND author_id = $book_author
AND author_id1 = $book_author1
AND author_id2 = $book_author2
AND author_id3 = $book_author3
AND author_id4 = $book_author4
AND author_id5 = $book_author5
AND author_id6 = $book_author6"
);
if (mysqli_num_rows($fetch_stock) > 0) {
echo "UPDATE book_stock SET stock_count = stock_count + 1 WHERE book_name = '$book_name' AND author_id = $book_author AND author_id1 = $book_author1 AND author_id2 = $book_author2 AND author_id3 = $book_author3 AND author_id4 = $book_author4 AND author_id5 = $book_author5 AND author_id6 = $book_author6";
mysqli_query($connection,
"UPDATE book_stock
SET stock_count = stock_count + 1
WHERE book_name = '$book_name'
AND author_id = $book_author
AND author_id1 = $book_author1
AND author_id2 = $book_author2
AND author_id3 = $book_author3
AND author_id4 = $book_author4
AND author_id5 = $book_author5
AND author_id6 = $book_author6"
);
} else {
echo "INSERT INTO book_stock (book_name, author_id, author_id1, author_id2, author_id3, author_id4, author_id5, author_id6, stock_count) value ('$book_name', $book_author,$book_author1, $book_author2, $book_author3, $book_author4, $book_author5, $book_author6, 1)";
mysqli_query($connection,
"INSERT INTO book_stock
(book_name, author_id, author_id1, author_id2,
author_id3, author_id4, author_id5, author_id6,
stock_count)
value ('$book_name', $book_author,$book_author1, $book_author2,
$book_author3, $book_author4, $book_author5, $book_author6, 1)");
}
When i print query Its look like this
INSERT INTO book_stock
(book_name, author_id, author_id1, author_id2, author_id3,
author_id4, author_id5, author_id6, stock_count)
value ('cat demo1', 1,2, 3, 4, NULL, NULL, NULL, 1)
I got solution for this from 1 website, which is
$fetch_stock = mysqli_query($connection,
"SELECT *
FROM `book_stock`
WHERE book_name = '$book_name'
AND author_id = $book_author
AND IFNULL(author_id1, 0)= IFNULL($book_author1, 0)
AND IFNULL(author_id2, 0)= IFNULL($book_author2, 0)
AND IFNULL(author_id3, 0)= IFNULL($book_author3, 0)
AND IFNULL(author_id4, 0)= IFNULL($book_author4, 0)
AND IFNULL(author_id5, 0) = IFNULL($book_author5, 0)
AND IFNULL(author_id6, 0) = IFNULL($book_author6, 0)"
);
Its working when i want to display stock.
But i want to do a crud operation by using this logic.
I tried to do from above solution, its work fine but, in db table its update null with 0.
which is not got as per me.
please guide me with best solution.
Thanks
I am trying to update the record of Employee.MY query shows the message "Employee record updated Successfully" but it is not updating in table My code goes like this
{
$eid=intval($_GET['uin']);
$uin=$_POST['uin'];
$fname=$_POST['firstName'];
$lname=$_POST['lastName'];
$email=$_POST['email'];
$department=$_POST['department'];
$recoffr=$_POST['recoffr'];
$mobileno=$_POST['mobileno'];
$sql="
update tblemployees
set FirstName = :fname
, LastName = :lname
, email = :email
, department = :department
, recoffr = :recoffr
, Phonenumber = :mobileno
where uin = :eid
";
$query = $dbh->prepare($sql);
$query->bindParam(':uin',$uin,PDO::PARAM_STR);
$query->bindParam(':fname',$fname,PDO::PARAM_STR);
$query->bindParam(':lname',$lname,PDO::PARAM_STR);
$query->bindParam(':email',$email,PDO::PARAM_STR);
$query->bindParam(':department',$department,PDO::PARAM_STR);
$query->bindParam(':recoffr',$recoffr,PDO::PARAM_STR);
$query->bindParam(':mobileno',$mobileno,PDO::PARAM_STR);
$query->execute();
$msg="Employee record updated Successfully";
}
My table structure is Table structure
You have assigned Uin to wrong Id
$query->bindParam(':eid',$uin,PDO::PARAM_STR);
not
$query->bindParam(':uin',$uin,PDO::PARAM_STR);
you can try this:
if (isset($_GET['uin'])) {
$ID = $_GET['uin'];
} else {
$ID = "";
}
$tblemployees_data = array();
$sql_query = "SELECT firstName, lastName, email, department, recoffr, mobileno
FROM tblemployees
WHERE uin = ?";
if ($query_category->prepare($sql_query)) {
// Bind your variables to replace the ?s
$query_category->bind_param('s', $ID);
// Execute query
$query_category->execute();
// store result
$query_category->store_result();
$query_category->bind_result($previous_category_image);
$query_category->fetch();
$query_category->close();
}
I need help with update my column in database. I use InnoDB, probably problem is here
$sql_update_heslo = "UPDATE users SET u_password = $_noveHeslo WHERE u_name = '$_SESSION[username]'";
first I am checking if Button was clicke. If yes, then I am checking if there is only 1 user with this name who is logged in, then I am checking if MD5 password from the database is same as user input, if yes then update password based on the user entry.
if (isset($_POST['pass_aktualizovat'])) {
$_old_password = md5($_POST['o_pass']);
$sql_search_for_all_userss = "SELECT * FROM users WHERE u_name = '$_SESSION[username]' ";
$result = mysqli_query($connect_to_db, $sql_search_for_all_userss);
// ak sa najde jedna zhoda v databazy
if ($db_data = mysqli_num_rows($result) == 1) {
while (mysqli_fetch_assoc($result)) {
$_aktualneHeslo = $db_data['u_password'];
}
if (md5($_POST['o_pass'])==$_aktualneHeslo) {
$_noveHeslo = md5($_POST['n_pass']);
$sql_update_heslo = "UPDATE users SET u_password = '$_noveHeslo' WHERE u_name = '".$_SESSION['username']."'";
mysqli_query($connect_to_db, $sql_update_heslo);
echo "treti";
}
echo "druhy";
}
echo "prvy";
}
?>
I have sql + php query and i need inform user when update fail exmpl:
$sql = "UPDATE db SET
date = GetDate(),
...
...
...
WHERE name = '$name1' and code = '$code' and value1 = '$value1' and value2='$value2'
";
sqlsrv_query( $con, $sql);
And now if php variables values not 100% match values in db update fails but users cant see that. He can check records and try again. I would like inform him when query update nothing.
Like GOB commented, you can use the PHP sqlsrv_rows_affected function to retrieve the number of affected rows. For example:
$stmt = sqlsrv_query( $conn, $sql , $params, $options );
$row_count = sqlsrv_rows_affected( $stmt );
if ($row_count === false)
echo "Error in retrieving row count.";
else
echo $row_count;
Before directly executing update query,check whether condition in update query exists or not. This can be done by selecting count of that condition.
Try below code:
$sql = "select count(*) as count from db WHERE name = '$name1' and code = '$code' and value1 = '$value1' and value2='$value2' ";
while($row = mysqli_fetch_array($sql))
{
$count = $row['count'];
}
if($count == 0)
{
echo 'update will fail';
}
else
{
$sql = "UPDATE db SET
date = GetDate(),
...
...
...
WHERE name = '$name1' and code = '$code' and value1 = '$value1' and value2='$value2'
";
}
I have been going over this for a few days now and keep reaching stumbling blocks. I am trying to select a unique id from table2 and match it against an id in table1. If the id is matched, update the row in table1 and remove the record from table2. If there is no match then insert the record into table1.
I have got to a point where I can update the records and insert the new ones but I cannot seem to delete the matched record before the insert therefore creating a duplicate. I have tried a delete join query after the update but because the new logon_id has a character prepended to it, it no longer matches in table2.
I was doing an update join before for updates but I had too many queries going on so trying to keep it simple.
Any advice? Still a newb at this game.
$table2_query = "SELECT * FROM table2";
$table2_result = mysql_query($table2_query);
$table2_count = mysql_num_rows($table2_result);
if($table2_count == 0) {
if(mysql_error()) {
echo 'Error: '.mysql_error();
}
}
while($table2_row = mysql_fetch_array($table2_result, MYSQL_ASSOC)) {
$check_number_length = strlen($table2_row['unique_id']);
if($check_number_length < 7) {
if(substr($table2_row['unique_id'], 0, 2) < 35) {
$logon_id = 'n' . $table2_row['unique_id'];
}else {
$logon_id = 'v' . $table2_row['unique_id'];
}
}else {
$logon_id = $table2_row['unique_id'];
}
// Set variables for insert query for creation of a new user record
$first_name = $table2_row['firstname'];
$last_name = $table2_row['lastname'];
$email_address = $table2_row['email'];
$duplicates_query = "SELECT * FROM table1 WHERE '$logon_id' = logon_id";
$duplicates_result = mysql_query($duplicates_query);
$duplicates_row = mysql_fetch_array($duplicates_result);
$duplicates_count = mysql_num_rows($duplicates_result);
if($duplicates_count == 0) {
if(mysql_error()) {
echo 'Error: '.mysql_error();
}
}else {
$update_records_query = "UPDATE table1 SET first_name='$first_name', last_name='$last_name', email_address='$email_address'";
$update_records_result = mysql_query($update_records_query);
$update_records_count = mysql_affected_rows();
echo $update_records_count;
}
$create_records_query = "INSERT INTO table1 (first_name, last_name, email_address) VALUES ('$first_name', '$last_name', '$email_address')";
$create_records_result = mysql_query($create_records_query);
$create_records_count = mysql_affected_rows();
if($create_records_count == 0){
if(mysql_error()){
echo 'Error: '.mysql_error();
}
}
echo $create_records_count . ' record(s) created.';
}
$res = mysql_query ("SELECT count(table1.login_id) AS count FROM table2 LEFT JOIN table1 ON table2.login_id = table1.login_id WHERE table2.login_id = \"".$login_id."\";") or die ("Error joining tables");
/*joins both tables together and selects both id's from tables */
$row = mysql_fetch_assoc($res); // should only return one row so grab first
if ($row['count'] > 0) { // check if id exists in both tables (duplicates)
// updates rows from one table into another
mysql_query('UPDATE table1,table2 SET table1.username = table2.username, table1.password = table2.password, table1.email = table2.email WHERE table1.login_id = "'.$login_id.'" AND table2.login_id = "'.$login_id.'";') or die ("error updating table1");
//delete old row
mysql_query('DELETE FROM table2 WHERE login_id = "'.$login_id.'";') or die("error deleting from table2");
}else { // if id doesn't exist in table1
mysql_query ("INSERT INTO table1(username,password,email) SELECT username,password,email FROM table2 where login_id = '".$login_id."';") or die ("error inserting into table1");
}
remove the n at the beginning of login_id on table1
UPDATE table1 set login_id = replace(login_id,"n","");
new update query if remove n at begin of login_id
mysql_query ('UPDATE table1,table2 SET table1.username = table2.username, table1.password = table2.password, table1.email = table2.email WHERE table1.login_id = table2.login_id AND table1.login_id = "'.$login_id.'";');