MySQL insert does not work - php

I want to insert data in mysql database.when i am trying to insert my query using PHP MyAdmin then work but if i am tring to insert from my php site from submission the not work some text not insert.
my text
http://www.lexisnexis.com/hottopics/gacode/
§ 16-11-125.1. Definitions
As used in this part, the term:
my form submission insert only this text
http://www.lexisnexis.com/hottopics/gacode/
my query
$test='http://www.lexisnexis.com/hottopics/gacode/
§ 16-11-125.1. Definitions
As used in this part, the term:';
$conn = mysql_connect('localhost', 'test', 'test') or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
mysql_query('SET NAMES utf8');
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET SESSION collation_connection ='utf8_unicode_ci'");
$queryc = "INSERT INTO `table` (data17)values ('".addslashes($test)."')";
mysql_query($queryc) or die(mysql_error());

for the database connection, it should be:
$conn = mysqli_connect('localhost', 'test', 'test') or die(mysqli_error($conn));
for the query, it should be:
mysqli_query($conn, $queryc) or die(mysqli_error($conn);
remember, the mysqli_query's arguments are database connection, query
also, it would be better if the query ($queryc) was:
$queryc = "INSERT INTO `table` (data17)values mysqli_real_escape_string($test))";

Related

bind_param does not save special characters correctly

I need to use bind_param to get away with data injection. When I use bind_param, special characters such as € or ب are being saved in mysql as صذق.
1-I am sure mysql table is set up correctly.
2-I have changed the word processor's encoding to UTF8.
3-I have included many utf8 character sets all over the place.
Any thoughts how to fix this? or maybe I should start using other methods such as mysqli_real_escape_string?
($_POST["post_word"] is generated in a separate page)
$connect = mysqli_connect("r"," r","r","r"); //not real data here
$connect->set_charset('utf8');
$connect->query("SET NAMES utf8");
$querye= mysql_query("SET NAMES utf8");
mysqli_query($connect,"INSERT INTO wordtable ( wdate) VALUES (CURRENT_TIMESTAMP())");
$sqlz = "SELECT wid FROM wordtable ORDER BY wdate DESC LIMIT 1";
$resultz = mysql_query($sqlz);
$rowz = mysql_fetch_array($resultz);
$wid=$rowz['wid'];
$mysqli = new mysqli(HOSTNAME, MYSQLUSER, MYSQLPASS, MYSQLDB);
$connect->set_charset('utf8');
$unsafe_variable = $_POST["post_word"];
$stmt = $mysqli->prepare("UPDATE wordtable SET word=(?)
WHERE wid='$wid' ");
$stmt->bind_param("s", $unsafe_variable);
$stmt->execute();
$stmt->close();
$mysqli->close();
Would it be perhaps
$querye= mysql_query("SET NAMES utf8");
Your querying with MySQL here

change mysql table character set

I have a table that data was inserted in it some time ago. data included by php and character set in it was utf-8 .
mysql_query("set names utf8");
now ,I need this table in another project which all data will show on php mysql default character set.
problem : my data is persian, when I set charachter set utf-8, every things is ok, but without character set data convert to the "?????" what should I do?!
I want to import all data form old table to new table on new character set !!
Try this might help you
<?php
header('Content-Type: text/html; charset=utf-8');
?>
and then in the connection
<?php
$dbLink = mysql_connect($argHost, $argUsername, $argPassword);
mysql_query("SET character_set_results=utf8", $dbLink);
mb_language('uni');
mb_internal_encoding('UTF-8');
mysql_select_db($argDB, $dbLink);
mysql_query("set names 'utf8'",$dbLink);
?>
///
$SERVER = "http://localhost:8084";
$db = mysql_connect('localhost','root') or die("Not Connected");
$sldb = mysql_select_db('vifarbydata',$db);
mysql_query("set names utf8");
$sql = "SELECT * FROM `region` ";
$res = mysql_query($sql);
while($h=(mysql_fetch_array($res)))
{
$row[] = $h;
}
mysql_close($db);
/// set another character set
$SERVER = "http://localhost:8084";
$db = mysql_connect('localhost','root') or die("Not Connected");
$sldb = mysql_select_db('vifarbydata',$db);
foreach ($row as $item)
{
echo $sql = "insert into `region2` (id,name,myId) values (".$item['id'].",'".$item['name']."','".$item['myId']."') ";
mysql_query($sql);
}
#afsane, using mysqldump, you can dump the data from old_table and then import it into the new_table which has the new character encoding.
mysqldump -u user -p --no-create-info schema old_table > old_table.dump
mysql -u user -p schema new_table < old_table.dump
This will be much faster than doing a conversion via PHP.

$__POST[string] to receive a unicode string fails

Unable to perform the query statement Incorrect string value: \x96\xBC\x91O\x82\xCD... for column 'member' at row 1.
Here is the code.
$member=stripslashes($_POST["membername"]);
$link=mysql_connect(localhost,$username, $password) or die("unable to connect to database ".mysql_error());
mysql_set_charset('utf8', $link);
mysql_selectdb($database) or die("unable to select db ".mysql_error());
$query="SELECT*FROM members";
$result=mysql_query($query);
$rowNum=mysql_num_rows($result)+1;
$query="INSERT INTO members VALUES ('$rowNum','$member','$memberemail','$memberpass','$memberRegDate','$memberRegDate','$memberIP')";
mysql_query($query) or die("Unable to perform the query statement. ".mysql_error());
EDIT; I already change my DB as this
ALTER DATABASE users CHARACTER SET UTF8 COLLATE UTF8_UNICODE_CI;
and
ALTER TABLE members CHANGE name name varchar(100) character set utf8;
That's not UTF-8. You should be looking at the HTTP headers in the request to see what charset is being used, and convert appropriately.
>>> print '\x96\xBC\x91O\x82\xCD...'.decode('cp949')
뼹멟궼...

Cant insert data into second database connection

See the code below, there are two databases connections.
First it get the data from first connection and then insert into second database connection but it will not insert - I can an error saying Unknown column 'fullname' in 'field list'
When I tried SQL query manually in phpMyAdmin and it work fine...
$db_new = mysql_connect('localhost', 'root', 'password');
if (!mysql_select_db("menu_new", $db_new)) {
die("Cant connect menu_new DATABASE");
}
$db_old = mysql_connect('localhost', 'root', 'password');
if (!mysql_select_db("old_menu", $db_old)) {
die("Cant connect old_menu DATABASE");
}
$SQL_old = "SELECT * FROM old_table";
$q = mysql_query($SQL_old, $db_old);
while ($row = mysql_fetch_assoc($q)) {
$name = $row['name'];
$SQL = "INSERT INTO tbl_name (fullname) values ('$name')";
//Problem Here - It wont insert into second database
mysql_query($SQL, $db_new) or die(mysql_error($db_new));
}
Nothing is strange in this behavior. Just add the $link parameter on your mysql_connect calls, and set it to true. By default it is False and it means that reusing this function with the same parameters, which is what you're doing as the db name is not on your mysql-connect, will reuse existing connexion with same parameters. So you have two variables but only one connexion.
It means you're simply moving the db used in this connexion. Prefixing with the db name fixed the problem as MySQL allow inter-base manipulations from the same connexion if it's on the same db server.
Thanks #Konerak for suggestion and that does work!
To Insert/Select data from Database connection, you will have to include database name before the table name.
For Example
From:
mysql_query("INSERT into tbl_name (fullname) values ('1')", $db_new) or die(mysql_error($db_new));
To:
mysql_query("INSERT into menu_new.tbl_name (fullname) values ('1')", $db_new) or die(mysql_error($db_new));
That is really odd though.

MySQL Transaction From PHP

QUERY 1:
$link = mysql_connect('localhost', 'root', '');
mysql_select_db('rems', $link);
mysql_query('SET AUTOCOMMIT=0; START TRANSACTION', $link);
mysql_query('DELETE FROM admins WHERE admin_id=4', $link);
mysql_query('ROLLBACK; SET AUTOCOMMIT=1', $link);
QUERY 2:
$link = mysql_connect('localhost', 'root', '');
mysql_select_db('rems', $link);
mysql_query('SET AUTOCOMMIT=0;START TRANSACTION;
DELETE FROM admins WHERE admin_id=4;
ROLLBACK; SET AUTOCOMMIT=1', $link);
From the above two queries the first one does not execute properly (transaction does not work) because of executing the queries separately by calling the mysql_query functions multiple times. But i need it do be done by this way. That is i need the result by the first way (calling mysql_query function several times for a single transaction)
Any IDEA please???
With the standard mysql module, either call every query seperately:
mysql_query('SET AUTOCOMMIT=0');
mysql_query('START TRANSACTION');
mysql_query('DELETE FROM admins WHERE admin_id=4');
mysql_query('ROLLBACK');//nothing will be done, I assume it's for testing
mysql_query('SET AUTOCOMMIT=1');
Or create a procedure first:
DELIMITER //
CREATE PROCEDURE weirdrolledbackdelete (IN oid INTEGER)
BEGIN
SET AUTOCOMMIT=0;
START TRANSACTION;
DELETE FROM admins WHERE id = oid;
ROLLBACK;
SET AUTOCOMMIT=1;
END;//
So you can use it later:
mysql_query('CALL weirdrolledbackdelete(4);');

Categories