gpt4 book ai didi

javascript - 使用 Ajax 和 PHP 将用户 ID 上传到数据库

转载 作者:太空宇宙 更新时间:2023-11-04 16:00:49 25 4
gpt4 key购买 nike

问题是我有一个页面,其中显示我的用户帖子,并且帖子上有一个表单输入形式的连接按钮。每篇帖子都有海报id。我希望当用户单击连接按钮时,海报的 id 会INSERTED 插入到数据库中。

PHP

<?php

$sql = <<<EOF
SELECT COUNT(*) as count FROM programs;
EOF;

$ret = $db->querySingle($sql);

if ($ret == 0)
{
echo "<div class='no_prog'>No programs currently advertised</div>";
}
else
{
$rsql = <<<EOF
SELECT * FROM programs ORDER BY id DESC;
EOF;


$rret = $db->query($rsql);

while ($rrow = $rret->fetchArray(SQLITE3_ASSOC))
{
$banner = $rrow['banner'];
$banner_2 = $rrow['banner_2'];
$title = $rrow['sem_title'];
$spons = $rrow['sem_spons'];
$link = $rrow['sem_link'];
$company = $rrow['sem_comp'];
$brief = $rrow['sem_brief'];
$ad_id = $rrow['userid'];

$usql = <<<EOF
SELECT * FROM User WHERE ID = '$ad_id';
EOF;
$uret = $db->query($usql);

while ($urow = $uret->fetchArray(SQLITE3_ASSOC))
{
$ad_img = $urow['image'];
$ad_name = $urow['fname'];


echo "<div class=\"preview_prog\">
<div class='posted_by'>
<p><img src='$ad_img'></p>
</div>
<div class='ad_user_info'>
<div class='ad_info'>
<div class='ad_info_img'>
<img src='$ad_img'>
</div>
<div class='ad_info_name'>
<p>$ad_name</p>
</div>
<div class='ad_info_conn'>
<div class='ad_info_conn_btn'>
<div class='conn_img'>
<img src='images/connect.png'>
</div>

//TOP INSERT $ad_id TO DATABASE

<form action='connect_exec.php' method='post' id='connect_form' enctype='multipart/form-data'>
<input type='submit' name='connect' class='conn_text' id='connect' value='connect +'>
<!--<a href='user_connect.php?userid=$ad_id' class='conn_text' name='user_connect'>Connect +</a>-->
</form>
</div>

我尝试将 form="action" 放入外部 php 文件中,但它没有获取$ad_id。很可能是因为它不是链接

Action PHP

<?php
require_once ("db.php");
$db = new MyDB();

session_start();

if (isset($_POST['connect']))
{
$my_id = $_SESSION['log_id'];
$ad_id = (int)$_GET['userid'];
$rand_num = rand();

$hsql =<<<EOF
SELECT COUNT(hash) as count FROM connect WHERE (user_one = '$my_id' AND user_two = '$ad_id') OR (user_one = '$ad_id' AND user_two = '$my_id');
EOF;

$hret = $db->querySingle($hsql);

if ($hret == 1)
{
$response = "Your are already connected to '$ad_id'";
}
else
{
$csql =<<<EOF
INSERT INTO connect (user_one, user_two, hash) VALUES ('$my_id', '$ad_id', '$rand_num');
EOF;
$cret = $db->exec($csql);

if (!$cret)
{
$message = "Error connecting to '$ad_id'";
}
else
{
$message = "Successfully Connected to '$ad_id'";
}
}
}
?>

请就如何将 $ad_id 上传到数据库提供任何建议。

请注意,我是 PHP 新手,对 Ajax 知之甚少

最佳答案

尝试在您在字符串中创建的名为“userid”的表单中添加一个隐藏字段,该字段的值中将包含您希望在action.php中使用 $_GET['userid 接收的 id '] 中将此 $_GET['userid'] 替换为 $_POST['userid']。您将获得用户ID

<?php

$sql = <<<EOF
SELECT COUNT(*) as count FROM programs;
EOF;

$ret = $db->querySingle($sql);

if ($ret == 0)
{
echo "<div class='no_prog'>No programs currently advertised</div>";
}
else
{
$rsql = <<<EOF
SELECT * FROM programs ORDER BY id DESC;
EOF;


$rret = $db->query($rsql);

while ($rrow = $rret->fetchArray(SQLITE3_ASSOC))
{
$banner = $rrow['banner'];
$banner_2 = $rrow['banner_2'];
$title = $rrow['sem_title'];
$spons = $rrow['sem_spons'];
$link = $rrow['sem_link'];
$company = $rrow['sem_comp'];
$brief = $rrow['sem_brief'];
$ad_id = $rrow['userid'];

$usql = <<<EOF
SELECT * FROM User WHERE ID = '$ad_id';
EOF;
$uret = $db->query($usql);

while ($urow = $uret->fetchArray(SQLITE3_ASSOC))
{
$ad_img = $urow['image'];
$ad_name = $urow['fname'];


echo "<div class=\"preview_prog\">
<div class='posted_by'>
<p><img src='$ad_img'></p>
</div>
<div class='ad_user_info'>
<div class='ad_info'>
<div class='ad_info_img'>
<img src='$ad_img'>
</div>
<div class='ad_info_name'>
<p>$ad_name</p>
</div>
<div class='ad_info_conn'>
<div class='ad_info_conn_btn'>
<div class='conn_img'>
<img src='images/connect.png'>
</div>

//TOP INSERT $ad_id TO DATABASE

<form action='connect_exec.php' method='post' id='connect_form' enctype='multipart/form-data'>
<input type='hidden' name='userid' value='$ad_id'/>
<input type='submit' name='connect' class='conn_text' id='connect' value='connect +'>
<!--<a href='user_connect.php?userid=$ad_id' class='conn_text' name='user_connect'>Connect +</a>-->
</form>
</div>

Action.php

<?php
require_once ("db.php");
$db = new MyDB();

session_start();

if (isset($_POST['connect']))
{
$my_id = $_SESSION['log_id'];
$ad_id = (int)$_POST['userid'];
$rand_num = rand();

$hsql =<<<EOF
SELECT COUNT(hash) as count FROM connect WHERE (user_one = '$my_id' AND user_two = '$ad_id') OR (user_one = '$ad_id' AND user_two = '$my_id');
EOF;

$hret = $db->querySingle($hsql);

if ($hret == 1)
{
$response = "Your are already connected to '$ad_id'";
}
else
{
$csql =<<<EOF
INSERT INTO connect (user_one, user_two, hash) VALUES ('$my_id', '$ad_id', '$rand_num');
EOF;
$cret = $db->exec($csql);

if (!$cret)
{
$message = "Error connecting to '$ad_id'";
}
else
{
$message = "Successfully Connected to '$ad_id'";
}
}
}
?>

关于javascript - 使用 Ajax 和 PHP 将用户 ID 上传到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42295584/

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