I inserted data through the phpmyadmin into my table. It shows me the query it uses so I copied and pasted it into my php code.
我通过phpmyadmin将数据插入到我的表中。它向我展示了它使用的查询,因此我将其复制并粘贴到我的php代码中。
My Php code is suppose to be submitting a form and I am trying to get $_POST('name') into the query.When I run the code it fills everything out but the version_name field where name goes.
我的Php代码假设是提交一个表单,我试图将$ _POST('name')放入查询中。当我运行代码时,它会填充所有内容,但名称所在的version_name字段。
Funny thing is im using the same MYSQL query that inserts it correctly on the phpmyadmin.
有趣的是我使用相同的MYSQL查询,在phpmyadmin上正确插入它。
I have moved on to trying a random name not the POST to see if it submits but i keep getting a blank... any suggestions?
我已经继续尝试一个随机名称而不是POST,看看它是否提交,但我一直空白......有什么建议吗?
$sql = "INSERT INTO `Prototype`.`Version` ('version_id', `version_name`, `version_status`, `created_date`, `created_by`) VALUES ('','A', 'A', CURRENT_TIMESTAMP, NULL);";
this is the Insert part, and I have also tried removing the version_id since it is autoincramented but no help.
这是插入部分,我也尝试删除version_id,因为它是自动修改但没有帮助。
the first row is what the result looks like when submitted from my php. the second row is when I insert it right from phpmyadmin.
第一行是从我的PHP提交时的结果。第二行是我从phpmyadmin直接插入的。
Any help getting version_name to be submitted from my php would be wonderful!
任何帮助从我的PHP提交version_name将是很棒的!
edit----
Heres my php code
继承人我的PHP代码
<?php
// Retrieve form data
$name = $_POST['name'];
if (!$name) {
echo "save_failed";
return;
}
$db = array(
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'Prototype',
);
$link = @mysql_connect($db['host'], $db['login'], $db['password']);
if (!$link) {
echo "save_failed";
return;
}
mysql_select_db($db['database']);
// Clean variables before performing insert
$clean_name = mysql_real_escape_string($name);
// Perform insert
$sql = "INSERT INTO `Prototype`.`Version` (`version_name`, `version_status`, `created_date`, `created_by`) VALUES ( 'A', 'A', CURRENT_TIMESTAMP, NULL);";
if (@mysql_query($sql, $link)) {
echo "success";
@mysql_close($link);
return;
} else {
echo "save_failed";
@mysql_close($link);
return;
}
?>
3 个解决方案
#1
0
firs thing first $_POST('name');
is nothing $_POST['name'];
is right. Now answer to your question use following query.
第一个$ _POST('name');没有$ _POST ['name'];是正确的。现在使用以下查询回答您的问题。
$sql = "INSERT INTO `Prototype`.`Version` (`version_name`, `version_status`, `created_date`, `created_by`) VALUES ('A', 'A', CURRENT_TIMESTAMP, NULL)";
I have also checked it on my own table made from structure provided by you. it's query is given below.
我也在我自己的桌子上检查过你自己提供的结构。它的查询如下。
CREATE TABLE `proto` ( `version_id` int(3) NOT NULL AUTO_INCREMENT, `version_name` varchar(45) NOT NULL, `version_status` varchar(45) NOT NULL, `created_date` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `created_by` varchar(45) DEFAULT NULL, PRIMARY KEY (`version_id`)) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1
Below code is for you to submit it via php.
下面的代码是您通过php提交的。
$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');
$db = new PDO("mysql:host=".HOST.";dbname=".DBNAME.";charset=utf8", USERNAME, PASSWORD, $options);
$query = $db->prepare("INSERT INTO `Prototype`.`Version`
(`version_name`,
`version_status`,
`created_date`,
`created_by`)
VALUES
('A',
'A',
CURRENT_TIMESTAMP,
NULL)");
$result = $query->execute();
return $result ? true : false;
#2
0
This many be by the by, and not an answer to your question but in Mysql 4 this would work:
这个问题很多,而不是你的问题的答案,但在Mysql 4中这可行:
An auto increment value can be
自动增量值可以是
- left out completely, if you state named fields
- NULL
- 0
- '' (empty string)
完全省略,如果你陈述命名字段
''(空字符串)
From Mysql 5 the last one will not work.
从Mysql 5开始,最后一个不起作用。
It was an undocumented feature of Mysql 4 which was removed for Mysql 5.
它是Mysql 4的一个未记录的功能,它已被删除,用于Mysql 5。
#3
0
It was an error with the server. I deleted the database and started from scratch and it solved the issue. Almost went as far as uninstalling and reinstalling xampp.
这是服务器的错误。我删除了数据库并从头开始,它解决了这个问题。几乎到了卸载和重新安装xampp。
#1
0
firs thing first $_POST('name');
is nothing $_POST['name'];
is right. Now answer to your question use following query.
第一个$ _POST('name');没有$ _POST ['name'];是正确的。现在使用以下查询回答您的问题。
$sql = "INSERT INTO `Prototype`.`Version` (`version_name`, `version_status`, `created_date`, `created_by`) VALUES ('A', 'A', CURRENT_TIMESTAMP, NULL)";
I have also checked it on my own table made from structure provided by you. it's query is given below.
我也在我自己的桌子上检查过你自己提供的结构。它的查询如下。
CREATE TABLE `proto` ( `version_id` int(3) NOT NULL AUTO_INCREMENT, `version_name` varchar(45) NOT NULL, `version_status` varchar(45) NOT NULL, `created_date` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `created_by` varchar(45) DEFAULT NULL, PRIMARY KEY (`version_id`)) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1
Below code is for you to submit it via php.
下面的代码是您通过php提交的。
$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');
$db = new PDO("mysql:host=".HOST.";dbname=".DBNAME.";charset=utf8", USERNAME, PASSWORD, $options);
$query = $db->prepare("INSERT INTO `Prototype`.`Version`
(`version_name`,
`version_status`,
`created_date`,
`created_by`)
VALUES
('A',
'A',
CURRENT_TIMESTAMP,
NULL)");
$result = $query->execute();
return $result ? true : false;
#2
0
This many be by the by, and not an answer to your question but in Mysql 4 this would work:
这个问题很多,而不是你的问题的答案,但在Mysql 4中这可行:
An auto increment value can be
自动增量值可以是
- left out completely, if you state named fields
- NULL
- 0
- '' (empty string)
完全省略,如果你陈述命名字段
''(空字符串)
From Mysql 5 the last one will not work.
从Mysql 5开始,最后一个不起作用。
It was an undocumented feature of Mysql 4 which was removed for Mysql 5.
它是Mysql 4的一个未记录的功能,它已被删除,用于Mysql 5。
#3
0
It was an error with the server. I deleted the database and started from scratch and it solved the issue. Almost went as far as uninstalling and reinstalling xampp.
这是服务器的错误。我删除了数据库并从头开始,它解决了这个问题。几乎到了卸载和重新安装xampp。