gpt4 book ai didi

Saving textbox values to database(将文本框值保存到数据库)

转载 作者:bug小助手 更新时间:2023-10-24 22:42:06 25 4
gpt4 key购买 nike



I'm kinda new to PHP and MySQL.

我对PHP和MySQL还是个新手。



<p>Pickup Date: <input type="date" name="date"></p>
<input type='submit' name='submit' value='Secure Order!'>


So I have his part in my code. I want the date to be saved in my database. In my database I have a table named date with columns 'date' and 'account name'. I want that when I click the 'secure order' button, the date in the textbox saves in the database. Also, the current user logged in would also be saved in account name.

所以我的代码里有他的角色。我希望日期保存在我的数据库中。在我的数据库中,我有一个名为Date的表,其中有‘Date’和‘Account Name’列。我希望当我点击“安全顺序”按钮时,文本框中的日期会保存在数据库中。此外,登录的当前用户也将保存在帐户名称中。


更多回答

What have you tried? How are you storing the details of the currently logged in user?

你都试了些什么?您如何存储当前登录用户的详细信息?

The library you want to look at is php's mysqli. That allows you to create queries to insert the data. I would like to note that date is a keyword in mysql so I would recommend changing the name of the column to something slightly different.

您要查看的库是php的mysqli。这允许您创建查询以插入数据。我想指出的是,Date是MySQL中的一个关键字,因此我建议将该列的名称更改为稍微不同的名称。

优秀答案推荐

on your form suppose you have:

在您的表单上,假设您有:



<form id="myform" action="myphppage.php" method="post">


now when you hit submit the data in the input will be posted to myphppage.php.

现在,当您点击Submit时,输入中的数据将被发布到myphppage.php。



Now you need to write myphppage.php.

现在您需要编写myphppage.php。



At the top you will have something like:

在顶部,您将看到类似以下内容:



<?php
$account = $_POST['account'];
$mydate = $_POST['date'];
$query="INSERT INTO date (date,account name) VALUES($mydate,$account)";
//Use mysqli object to execute query.
?>


I haven't used the mysqli library because I got used to using the old mysql_ libraries but I got yelled at for suggesting it since it is now deprecated.

我还没有使用mysqli库,因为我已经习惯了使用旧的mySQL_库,但我因为建议使用它而受到抨击,因为它现在已经不受欢迎了。



This should get you started.

这应该是你开始的第一步。



It is also common to have the form post back to itself with the data. In which imagine your form is on the page "myphppage.php" then you could have the following at the top:

将表单与数据一起发送回自身也是很常见的。其中,假设您的表单位于页面“myphppage.php”上,则可以在顶部显示以下内容:



<?php 
if(isset($_POST['submit'])){
//continue with the php code from above here
}
?>


Why are you using double quotes on your first <input> and then simple quotes on your second <input>? Most of the time, in HTML, we will encourage the use of double quotes.

为什么在第一个<输入>中使用双引号,然后在第二个<输入>中使用单引号?在大多数情况下,在HTML中,我们会鼓励使用双引号。



Also, there are a lot of missing parts for this form to be correct. Here is just an example of how to use the variable typed in the input:

此外,为了使这张表格正确,还有很多缺失的部分。以下只是一个如何使用输入中键入的变量的示例:



<?php
if(isset($_POST['date'])) {
echo "You picked ", $_POST['date'], "\n"
}
?>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<p>Pickup Date: <input type="date" name="date"></p>
<input type="submit" value="Secure Order!">
</form>


As for the MySQL part, you will need to learn and try a bit by yourself. Start there: http://www.php.net/manual/en/book.mysqli.php

至于MySQL部分,你需要自己学习和尝试一下。从那里开始:http://www.php.net/manual/en/book.mysqli.php



<?php 
if(isset($_POST['submit'])){
// you can get the logged in user from the session
// this means when your user is logged in, you need to set the
//session
$accountId = $_SESSION['userId'];
$date = filter_input(INPUT_POST, 'date');
$sql= "INSERT INTO date (`date`, `account Id`) VALUES($date,$accountId)";
//for this line to work, you need to store your database connection to mysqli in $mysqli
if ($mysqli→query($sql)) {
printf("Record inserted successfully.<br />");
}
if ($mysqli→errno) {
printf("Could not insert record into table: %s<br />", $mysqli→error);
}
$mysqli→close();
}else{
?>
<html>
<head>
<title>Add New Record in MySQL Database</title>
</head>
<body>
<form method = "post" action = "<?php $_PHP_SELF ?>">
<p>Pickup Date: <input type="date" name="date"></p>
<input type='submit' name='submit' value='Secure Order!'>
</form>
</body>
</html>
<?php
}
?>


You can use PHP $_POST SuperGlobal to do that :

您可以使用PHP$_POST SuperGlobal来执行此操作:



HTML:

Html:



<input type="text" name="username" />


PHP-onsubmit

Php-onmit



<?php

if(isset($_POST['submit'])
{
$username= $_POST["username"];
$date= $_POST["date"];

// your insert code //

}
?>

更多回答

This isn't a complete answer. He also needs some sort of example for the mysql query that would go in place of your comment.

这并不是一个完整的答案。他还需要一些MySQL查询的示例来代替您的注释。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com