doufeng2877 2014-09-10 14:24
浏览 102
已采纳

使用x-editable将URL文本插入数据库

I have a table with inline editing using X-editable and everything is working fine including the value being submitted to the database, but for some reason it will display my echo in the else section.

Here is my PHP code:

require("config.php");

$userid = $_SESSION['user']['id'];
$sql = "SELECT fb_url, tw_url, ggl_url FROM social_preferences WHERE user_id = :userID";
$stmt = $db->prepare($sql);
$stmt->bindParam(":userID", $userid, PDO::PARAM_INT);
$stmt->execute();
$result = $stmt->fetch();   


$pk = $_POST['pk'];
$name = $_POST['name'];
$value = $_POST['value'];

    if(!empty($value)) {
            try // save user selection to the database
              {

                $stmt = $db->prepare("UPDATE social_preferences SET tw_url = :twurl WHERE user_id = :userID");
                $stmt->bindParam(":userID", $pk, PDO::PARAM_INT);
                $stmt->bindParam(':twurl', $value);
                $stmt->execute();

                header("Location: admin-social.php"); 
                die("Redirecting to admin-social.php"); 
               }  catch(PDOException $e) { echo 'Connection failed: ' . $e->getMessage(); }
    }else {
        echo 'Something went wrong!';
        var_dump($value);
    }

Here is my HTML code:

<a name="tw-url" id="tw-url" data-type="text" data-pk="<?php echo ($userid);?>" title="Edit"><?php echo ($result['tw_url']);?></a>

Like I said above everything seems to be working but it redirects to a page that will display my echo Something went wrong!even though it submitted the value to the DB. I included the var_dump to see if there is a value and that returns NULL. Can someone please help me? Any ideas why it would submit the right value to the database but redirect to my error?

Also, at what point does it send it to the database? I have a table in a form with a save button, but when I open the editable text and submit the new value does it send to the database when I save from the pop-over or when I click the save button in my table form?

展开全部

  • 写回答

1条回答 默认 最新

  • dongyiba8082 2014-09-19 08:23
    关注

    The else statement is executing because when your details inserted into DB, you have set a header which redirects to the same page, in that case the variable $value value set to empty and your else statement executes.

    The above answer is only valid if you set your header to same page.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部