I want Data insert into mysql using PHP. I am writing script HTML, PHP and I create Mysql database. I don't understand data not send mysql server. Here is the HTML and PHP code:
<?php
if (isset($_POST['submit'])){
$con = mysql_connect("localhost","root","");
if(!$con){
die("Can not connect: " . mysql_error());
}
//Database Connection.
mysql_select_db("learnarabic",$con);
$sql = "INSERT INTO article(ArticleDate,ArticleAuthor,ArticleSubject,ArticleSource,ArticleLevel,ArticleTitleEnglish,ArticleTitleArabic,ArticleFree,ArticleEnglish,ArticleTranslationEnglish,ArticleArabic,ArticleVowels,ArticleTransliteration,ArticleAudio)
VALUES
('$_POST['ArticleDate']',
'$_POST['ArticleAuthor']',
'$_POST['ArticleSubject']',
'$_POST['ArticleSource']',
'$_POST['ArticleLevel']',
'$_POST['ArticleTitleEnglish']',
'$_POST['ArticleTitleArabic']',
'$_POST['ArticleFree']',
'$_POST['ArticleEnglish']',
'$_POST['ArticleTranslationEnglish']',
'$_POST['ArticleVowels']',
'$_POST['ArticleTransliteration']',
'$_POST['ArticleAudio']',
'$_POST['ArticleArabic']')";//Insert data into Mysql.
mysql_query($sql,$con);
mysql_close($con);//Connection Close.
}
?>
Here is the SQL Database code:
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
--
-- Database: `learnarabic`
--
-- --------------------------------------------------------
--
-- Table structure for table `article`
--
CREATE TABLE IF NOT EXISTS `article` (
`articleid` int(250) NOT NULL AUTO_INCREMENT,
`ArticleDate` date NOT NULL,
`ArticleAuthor` varchar(250) NOT NULL,
`ArticleSubject` varchar(250) NOT NULL,
`ArticleSource` varchar(250) NOT NULL,
`ArticleLevel` varchar(250) NOT NULL,
`ArticleTitleEnglish` varchar(250) NOT NULL,
`ArticleTitleArabic` varchar(250) NOT NULL,
`ArticleFree` varchar(250) NOT NULL,
`ArticleEnglish` longtext NOT NULL,
`ArticleTranslationEnglish` longtext NOT NULL,
`ArticleArabic` longtext NOT NULL,
`ArticleVowels` longtext NOT NULL,
`ArticleTransliteration` longtext NOT NULL,
`ArticleAudio` blob NOT NULL,
PRIMARY KEY (`articleid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
I need a solution.
Try removing the $con variable from the mysql_query() function eg mysql_query($sql); also add single quotes to any text inside $_POST like $_POST['something']
Related
I only want to create the table once if it doesn't exist. Will this code create a new table every time? If yes, how do I prevent it?
$queryCreateUsersTable = "CREATE TABLE IF NOT EXISTS EmailList (
`ListName` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`FirstName` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`Email` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`ID` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`IP` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`ConfirmedEmail` tinyint(1) DEFAULT NULL,
`Subscribed` tinyint(1) DEFAULT 0,
UNIQUE KEY `ID` (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;";
if(!$conn->query($queryCreateUsersTable))
{
echo "Table creation failed: (" . $dbConnection->errno . ") " . $dbConnection->error;
}
CREATE TABLE IF NOT EXISTS means it will only create the table if it is not present.
Exactly as what CREATE TABLE IF NOT EXISTS means. Create a table if table does not exist.
Therefore, no it does not.
I have been working on a project on my localhost and I am wanting to use the database structure that my local app is using on my live site. I have done a mysql dump and tried adding it to my pdo db creation script(on live site) but when the db is created their is no structure. Im not sure what im doing wrong and am having trouble finding any examples of it. Im not quite sure what im missing any pointers would be great. Thanks in advance
code ex.
$host="localhost";
$root="root";
$root_password="root";
$user='newuser';
$pass='newpass';
$db= 'companyid' . $companyid;
try {
$dbh = new PDO("mysql:host=$host", $root, $root_password);
$dbh->exec("CREATE DATABASE `$db`;
CREATE USER '$user'#'localhost' IDENTIFIED BY '$pass';
GRANT ALL ON `$db`.* TO '$user'#'localhost';
FLUSH PRIVILEGES;
CREATE TABLE IF NOT EXISTS `address` (
`address` varchar(50) COLLATE latin1_general_ci NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
-- --------------------------------------------------------
--
-- Table structure for table `checkpics`
--
CREATE TABLE IF NOT EXISTS `checkpics` (
`image` varchar(120) COLLATE latin1_general_ci NOT NULL,
`amount` decimal(10,0) NOT NULL,
`date` date NOT NULL,
`photosmall` varchar(64) COLLATE latin1_general_ci NOT NULL,
`photobig` longblob NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`customerid` int(10) NOT NULL,
`checknumber` int(10) NOT NULL,
`checkamount` varchar(10) COLLATE latin1_general_ci NOT NULL,
`datepaid` varchar(10) COLLATE latin1_general_ci NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
-- --------------------------------------------------------
--
-- Table structure for table `CityStateZip`
--
CREATE TABLE IF NOT EXISTS `CityStateZip` (
`city` varchar(30) COLLATE latin1_general_ci NOT NULL,
`state` varchar(2) COLLATE latin1_general_ci NOT NULL,
`zip` varchar(10) COLLATE latin1_general_ci NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;")
or die(print_r($dbh->errorInfo(), true));
} catch (PDOException $e) {
die("DB ERROR: ". $e->getMessage());
}
Ok I figured out the problem. When creating multiple tables you must run separate commands or use a mysqli multi query command and separate each instance via a ;
I am trying to read from a database in MySQL and insert my data in another database in MySQL .
my first table is like this
CREATE TABLE IF NOT EXISTS `link` (
`_id` bigint(20) NOT NULL AUTO_INCREMENT,
`country` varchar(30) COLLATE utf8 DEFAULT NULL,
`time` varchar(20) COLLATE utf8 DEFAULT NULL,
`link` varchar(100) COLLATE utf8 DEFAULT NULL,
PRIMARY KEY (`_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=6149 ;
and the second table is
CREATE TABLE IF NOT EXISTS `country` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Name` varchar(15) CHARACTER SET utf8 NOT NULL,
`Logo` varchar(50) CHARACTER SET utf8 DEFAULT NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `Name_3` (`Name`),
UNIQUE KEY `ID` (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=8457 ;
There are about 6114 rows in first table that I'm trying to insert to second using this code
<?php
$tmp = mysqli_connect(******, *****, ****, *****); // First table in here
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$main = mysqli_connect(*****, *****, ****, ******); //Second table in here
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$req = "SELECT country FROM link";
$result = mysqli_query($tmp, $req) or die( mysqli_error($tmp) );
echo "-> ".mysqli_num_rows($result)."<br>";
while ($row = mysqli_fetch_array($result)) {
$con = $row["country"];
$req = "INSERT IGNORE INTO country (Name) VALUES ('$con')";
mysqli_query($main, $req) or die( mysqli_error($main) ) ;
}
?>
problem is the php code works but for 6114 take a very long time which I can't afford .
what is causing the code to take this long to work ? is it the "INSERT IGNORE" ?
is there any way I can do this faster ?
Since the databases are on the same server, you can simply use INSERT ... SELECT (which avoids having to bring the data into PHP and loop over the results executing separate database commands for each of them, so will be considerably faster):
INSERT INTO db2.country (Name) SELECT country FROM db1.link
you can try a create an index on column "country" of table Link.
I restarted the MySQL service and I attempted to use my PHP programs delete function to delete an existing row but I'm finding although the delete queries were counted the row was not deleted. I tried applying on delete cascade to the foreign key of the child table but that did not seem to have an effect. I'm wondering why the delete would be doing nothing.
CREATE TABLE `customers` (
`idcustomers` int(11) NOT NULL AUTO_INCREMENT,
`firstname` varchar(45) DEFAULT NULL,
`lastname` varchar(45) DEFAULT NULL,
`address1` varchar(45) DEFAULT NULL,
`address2` varchar(45) DEFAULT NULL,
`city` varchar(45) DEFAULT NULL,
`state` varchar(45) DEFAULT NULL,
`zip` varchar(45) DEFAULT NULL,
`phone` varchar(45) DEFAULT NULL,
`email` varchar(45) DEFAULT NULL,
`cell` varchar(45) DEFAULT NULL,
PRIMARY KEY (`idcustomers`),
UNIQUE KEY `idcustomers_UNIQUE` (`idcustomers`)
) ENGINE=InnoDB AUTO_INCREMENT=54 DEFAULT CHARSET=latin1
CREATE TABLE `events` (
`idevents` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(250) DEFAULT NULL,
`start` datetime DEFAULT NULL,
`end` datetime DEFAULT NULL,
`allday` varchar(50) DEFAULT NULL,
`url` varchar(1000) DEFAULT NULL,
`customerid` int(11) NOT NULL,
`memo` longtext,
`dispatchstatus` varchar(45) DEFAULT NULL,
PRIMARY KEY (`idevents`),
KEY `FK_events` (`customerid`),
CONSTRAINT `FK_events` FOREIGN KEY (`customerid`) REFERENCES `customers` (`idcustomers`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=latin1
Com_delete 2
The PHP looks like this:
<?php
session_start();
date_default_timezone_set("America/Los_Angeles");
if($_SESSION['loggedin'] != TRUE)
{
header("Location: index.php");
}
require_once('../php.securelogin/include.securelogin.php');
$mysqli = new mysqli($ad_host, $ad_user, $ad_password, "samedaycrm");
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$customerid = $_SESSION['customer_id'];
$tSQL = "delete from events where customerid = \"$customerid\"";
$result = $mysqli->query($tSQL);
$tSQL = "delete from customers where idcustomers = \"$customerid\"";
$result = $mysqli->query($tSQL);
echo $mysqli->error;
?>
Assuming that the customerid and idcustomers columns are both numeric it should be fine. You should not need to quote the variables in those queries btw, then you wouldnt need to escape them. You may try:
$tSQL = "delete from events where customerid = $customerid";
but it should not be any different than what you used already. Of course if you are not sure of the type of the column you can use:
$tSQL = "delete from events where customerid = '".$customerid."'";
or you can get away with:
$tSQL = "delete from events where customerid = '$customerid'";
but I have always hated that for some reason.
if all of that fails troubleshoot by spitting out the $customerid (or even the whole $tSQL) variable and then trying the query manually in phpmyadmin or toad or whatever db client you use, and see what it tells you. If it just says 0 rows affected, then run it like a select instead. Tailor to fit.
I use MySQL and PHP to store data in an Android app. I want to use UTF-8 in my database, and tables because the data is in English and Persian. This query is to create the table:
CREATE TABLE IF NOT EXISTS `tbl_notes` (
`user_id` int(11) NOT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
`note_subject` varchar(22) NOT NULL,
`note_date` text NOT NULL,
`note_content` varchar(21000) NOT NULL,
`note_deleted` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
I use this code before connecting to the database:
$con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysql_error());
$db = mysql_select_db(DB_DATABASE) or die(mysql_error());
mysql_query("SET CHARACTER SET 'UTF8'", $con);
mysql_query("SET NAMES utf8_general_ci", $con);
My problem is when I save the data to note_content and note_subject, the data is showing as "???????". And when I retrieve data to my android app again, the data is "????????".
How can I fix this error?